public class MapDemoActivity extends Activity {
Button capture;
ImageView image;
int cameracode=100;
Bitmap bm;
Boolean result;
FileOutputStream fos;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
capture=(Button)findViewById(R.id.capture);
capture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
image=(ImageView)findViewById(R.id.image);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameracode);
image.setDrawingCacheEnabled(true);
bm = image.getDrawingCache();
try {
fos = new FileOutputStream("sdcard/image.jpg");
result=bm.compress(CompressFormat.JPEG, 75, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==100)
{
bm=(Bitmap) data.getExtras().get("data");
image.setImageBitmap(bm);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
在这个编程中,我正在从相机拍摄图像。显示到图像视图然后我想 将其转换为Jpeg以存储在SD卡中... 但当我按下捕获图像按钮编程时,强行关闭.. &安培;在sdcard中创建的空jpeg文件...我想将jpeg文件存储到sdcard
答案 0 :(得分:1)
startActivityForResult
是一个异步调用,因此在等待捕获完成之前,会立即执行该调用之后的所有内容。您应该将保存代码移到onActivityResult
。
此外,您不应该对sdcard
进行硬编码,而应使用Environment.getExternalStorageDirectory()
代替。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
capture=(Button)findViewById(R.id.capture);
capture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
image=(ImageView)findViewById(R.id.image);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameracode);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==100)
{
bm=(Bitmap) data.getExtras().get("data");
image.setImageBitmap(bm);
if(bm == null) {
return; //probably user cancelled;
}
try {
fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "image.jpg"));
result=bm.compress(CompressFormat.JPEG, 75, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
最后,您可能想要在尝试保存之前检查您是否确实获得了图像,因为用户可能已取消图像捕获。
除此之外,我建议您发布完整的logcat堆栈跟踪。也许那里还有其他的东西。
答案 1 :(得分:0)
尝试将目标目录更改为:
try {
fos = new FileOutputStream("/mnt/sdcard/MyActivity/image.jpg");
result=bm.compress(CompressFormat.JPEG, 75, fos);
fos.flush();
fos.close();
确保在写入目录之前创建目录。
String dir = "/mnt/sdcard/MyActivity/";
directory = new File(dir);
if (!directory.exists()) {
directory.mkdirs();
}
答案 2 :(得分:0)
我认为执行startActivityForResult之后的代码部分,并且位图尚不可用。您应该将其直接保存在onActivityResult上。
此外,您是否曾要求Android通过提供以下意图来保存您的图像:
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) );
startActivityForResult(intent, TAKE_PHOTO_CODE);
getTempFile是一个函数,它返回我想要的路径。
PS:你有没有要求清单上的许可?你需要<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />