java - 图片保存在模拟器中但不在设备上

标签 java android android-emulator android-camera

我正在使用以下代码来保存/加载我的图片

 OnCreate(){
  super.onCreate();
  setContentView(...)
 ....
  setImage();   

  }
 protected void onResume(){
   super.onResume();
   setImage();  
   }


protected void onRestoreInstanceState(Bundle savedInstanceState){
   super.onRestoreInstanceState(savedInstanceState);
   setImage();

protected void onPostResume(){
   super.onPostResume();
   setImage();  

private void setImage(){
    if(loadPicture(getIntent().getStringExtra("position"),bitmap ) != null){
        Toast.makeText(this, "not null", Toast.LENGTH_SHORT).show();
          imageView.setImageBitmap(loadPicture(getIntent().getStringExtra("position"), bitmap));
    }
}
private void takePicture() {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File photo = new File(Environment.getExternalStorageDirectory(),
                "Pic.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, 0);

}

private void savePicture(String filename, Bitmap b, Context ctx){
    try {
           ObjectOutputStream bos;
           FileOutputStream out;// = new FileOutputStream(filename);
           out = ctx.openFileOutput(filename, Context.MODE_WORLD_READABLE);
           bos = new ObjectOutputStream(out);
           b.compress(Bitmap.CompressFormat.PNG, 100, bos);
          if(b.compress(Bitmap.CompressFormat.PNG, 100, bos) == true)
              Toast.makeText(this, "returned true", Toast.LENGTH_LONG).show();

          bos.flush();
        //  bos.close();
          out.close();
    } catch (Exception e) {
           e.printStackTrace();
    }
}
private Bitmap loadPicture(String filename, Bitmap b){
    //Drawable myImage = null;
    try {
        FileInputStream fis = openFileInput(filename);
        ObjectInputStream bis = null;
        try {
            bis = new ObjectInputStream(fis);
        } catch (StreamCorruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        // myImage = Drawable.createFromStream(ois, filename);
         b = BitmapFactory.decodeStream(bis);
        try {
            //bis.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    //return myImage;
    return b;

}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case 0:
        if (resultCode == Activity.RESULT_OK) {

            Uri selectedImage = imageUri;
            getContentResolver().notifyChange(selectedImage, null);

            ContentResolver cr = getContentResolver();

            try {
                bitmap = android.provider.MediaStore.Images.Media
                        .getBitmap(cr, selectedImage);

                imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, bitmap.getHeight()/2, bitmap.getWidth()/2, false));
                //previos imageView.setImageBitmap(bitmap)
                savePicture(getIntent().getStringExtra("position"), bitmap,
                        getApplicationContext());

                Toast.makeText(this, selectedImage.toString(),
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                        .show();



            }

这非常适合在模拟器中保存图像(即示例图像),但在真实手机中不起作用。在我拍完照片并按下手机上的“确定”图标后,出现大约 20 秒的黑屏,然后死机。

最佳答案

您应该尝试此处提供的一些解决方案

关于java - 图片保存在模拟器中但不在设备上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6880806/

相关文章:

java - 更改表结构但 hibernate 不更新它

java - 如何使用 json 文件显示自动完成字段的建议?

java - Sqlite 日期时间类型问题

java - 在Android Studio中导出应用程序时出现问题

java - 将文本换行到文本框中

linux - Android模拟器内存​​占用不断增加

c# - 无法在 Android 模拟器 xamarin 上发布 Hello World 应用程序

android - 如何在 Eclipse 中重构 R.java 的包

android - 如何处理 SearchView 中的后退箭头事件

android - 从 Android 将联系人插入 SIM