拍摄的图像未存储在Android上的特定文件夹 [英] Captured image is not stored in the specific folder in android
本文介绍了拍摄的图像未存储在Android上的特定文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个程序来捕捉图像,并且越来越被存储到SD卡/ DCIM /相机文件夹中。现在我想保存在SD卡中创建自己的目录所拍摄的图像,说/ somedir。
我能够使编程目录,但图像文件没有得到存储在其中。谁能告诉我在哪里,我做错了?这里是code ....
文件夹=新的文件(Environment.getExternalStorageDirectory()+/ ABC);位图mybitmap1; // mybitmap1包含图像。所以PLZ不认为我没有形象mybitmap1;
如果(!folder.exists())
{
成功= folder.mkdir();
Log.i(日志,创建的文件夹);
}
其他
{
Log.i(日志,文件夹已经在这里present!);
}
JPG字符串FNAME =日期+;
文件=新的文件(文件夹,FNAME);
如果(file.exists())
file.delete();
capturedImageUri = Uri.fromFile(文件); FileOutputStream中出;
字节[]的字节数组= stream.toByteArray();
尝试{ OUT =新的FileOutputStream(文件);
mybitmap1.com preSS(Bitmap.Com pressFormat.JPEG,100,出);
了out.flush();
out.close();
MediaStore.Images.Media.insertImage(getContentResolver(),mybitmap1,file.getName(),file.getName());
//MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
}赶上(例外五){
e.printStackTrace();
}
解决方案
请参考下面的code
公共无效的onActivityResult(INT申请code,INT结果code,意图数据){ super.onActivityResult(要求code,结果code,数据);
如果(结果code == RESULT_OK&放大器;&安培;请求code == 1){ 最后乌里selectedImage = data.getData(); 尝试{
位图= Media.getBitmap(getContentResolver(),selectedImage);
ByteArrayOutputStream字节=新ByteArrayOutputStream();
bitmap.com preSS(Bitmap.Com pressFormat.JPEG,90字节); 文件f =新的文件(Environment.getExternalStorageDirectory()
+文件分割符
+文件名);
f.createNewFile();
FileOutputStream中FO =新的FileOutputStream(F);
fo.write(bytes.toByteArray()); }赶上(FileNotFoundException异常五){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
}
}
I have created a program to capture the image and that is getting stored into sdcard/dcim/camera folder. Now I am trying to save the captured image in my own directory created in sdCard, say "/somedir".
I am able to make the directory programmatically but the image file is not getting stored in it.
Can anybody tell me where I am doing wrong here??
Here is the code....
File folder = new File(Environment.getExternalStorageDirectory() + "/abc");
Bitmap mybitmap1; //mybitmap1 contain image. So plz dont consider that I don't have image in mybitmap1;
if(!folder.exists())
{
success = folder.mkdir();
Log.i("Log", "folder created");
}
else
{
Log.i("Log", "Folder already present here!!");
}
String fname = date +".jpg";
file = new File( folder,fname);
if (file.exists ())
file.delete ();
capturedImageUri = Uri.fromFile(file);
FileOutputStream out;
byte[] byteArray = stream.toByteArray();
try {
out = new FileOutputStream(file);
mybitmap1.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
MediaStore.Images.Media.insertImage(getContentResolver(), mybitmap1, file.getName(), file.getName());
//MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
} catch (Exception e) {
e.printStackTrace();
}
解决方案
Refer the below code
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == 1 ) {
final Uri selectedImage = data.getData();
try {
bitmap = Media.getBitmap(getContentResolver(),selectedImage);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator
+ filename);
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这篇关于拍摄的图像未存储在Android上的特定文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文