Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 图片保存在emulator中,但不保存在设备上_Java_Android_Android Emulator_Android Camera - Fatal编程技术网

Java 图片保存在emulator中,但不保存在设备上

Java 图片保存在emulator中,但不保存在设备上,java,android,android-emulator,android-camera,Java,Android,Android Emulator,Android Camera,我正在使用以下代码保存/加载我的图片 OnCreate(){ super.onCreate(); setContentView(...) .... setImage(); } protected void onResume(){ super.onResume(); setImage(); } protected void onRestoreInstanceState(Bundle savedInstanceState){ super.

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

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

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


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

}


这非常适合在模拟器中保存图像(即示例图像),但在真实手机中不起作用。在我拍摄图像并按下手机上的“OK”图标后,屏幕会出现大约20秒的空白。然后它会崩溃。

您应该尝试这里提供的一些解决方案


您应该尝试这里提供的一些解决方案


能否粘贴logcat异常堆栈?如何检索logcat堆栈,因为它在模拟器上运行良好…在设备上,我怀疑这是一个超时,因为我感觉应用程序在经过一段时间后崩溃…这可能是什么原因?我想这与我保存可连接IDE的文件的方式有关您的设备使用usb电缆并启用设备上的调试。请参阅,问题已暂停(异常OutOfMemoryError)。我通过使用百万像素1而不是5来拍摄,确认图像文件太大。我如何修复此问题,并允许用户拍摄高分辨率照片?@Justin,你能发布一些示例代码吗?你能粘贴logcat异常堆栈吗?我如何检索logcat堆栈,因为它在模拟器上工作正常…在设备上,我怀疑是时候了退出,因为我觉得应用程序在长时间后崩溃…是什么原因造成的?我认为这与我保存文件的方式有关。您可以使用usb电缆将IDE连接到设备并启用设备上的调试。请参阅,问题已暂停(例外OutOfMemoryError)。我确认使用百万像素1而不是5拍摄的图像文件太大。如何修复此问题并允许用户拍摄高分辨率照片?@Justin,你能发布一些示例代码吗?很抱歉没有更新你,但我在早些时候使用了你提供给我的相同链接解决了此问题。回答很好,因为这是解决问题的方法我的问题!我很抱歉没有更新你,但我在早些时候用你提供给我的相同链接解决了这个问题。回答得很好,因为这是我问题的解决方案!
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();



            }