将图像保存在 android 中的单独目录中
Saving Image in a seperate directory in android
我可以从这段代码中提取图片,但我无法将图片保存到自定义目录中 Uri uriTarget = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
我认为这可能是我必须更改代码的地方
想创建一个自定义目录并将我的图像保存在那里
Camera.ShutterCallback myShutterCallback = new Camera.ShutterCallback(){
@Override
public void onShutter() {
// TODO Auto-generated method stub
}};
Camera.PictureCallback myPictureCallback_RAW = new Camera.PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
}};
Camera.PictureCallback myPictureCallback_JPG = new Camera.PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
Uri uriTarget = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(MainActivity.this,
"waH kiAa selfie hAi :D ",
//+ uriTarget.toString(),
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
}};
试试这个方法...
private void saveImage(byte[] arg0) {
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "YOUR_CUSTOM_FOLDER";
File myDir = new File(root);
myDir.mkdirs();
String fname = "YOUR_IMAGE_NAME" + ".jpg";
file = new File(myDir, fname);
if (file.exists()) {
file.delete();
}
Bitmap bitmap1 = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
FileOutputStream out;
try {
out = new FileOutputStream(file);
bitmap1.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
MediaStore.Images.Media.insertImage(activity.getContentResolver(), bitmap1, "", "");
}
我可以从这段代码中提取图片,但我无法将图片保存到自定义目录中 Uri uriTarget = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
我认为这可能是我必须更改代码的地方
想创建一个自定义目录并将我的图像保存在那里
Camera.ShutterCallback myShutterCallback = new Camera.ShutterCallback(){
@Override
public void onShutter() {
// TODO Auto-generated method stub
}};
Camera.PictureCallback myPictureCallback_RAW = new Camera.PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
}};
Camera.PictureCallback myPictureCallback_JPG = new Camera.PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
Uri uriTarget = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(MainActivity.this,
"waH kiAa selfie hAi :D ",
//+ uriTarget.toString(),
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
}};
试试这个方法...
private void saveImage(byte[] arg0) {
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "YOUR_CUSTOM_FOLDER";
File myDir = new File(root);
myDir.mkdirs();
String fname = "YOUR_IMAGE_NAME" + ".jpg";
file = new File(myDir, fname);
if (file.exists()) {
file.delete();
}
Bitmap bitmap1 = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
FileOutputStream out;
try {
out = new FileOutputStream(file);
bitmap1.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
MediaStore.Images.Media.insertImage(activity.getContentResolver(), bitmap1, "", "");
}