您好,我编写了这个简单的应用程序,我现在想要的是我的应用程序重新打开时显示我最后点击的最后一个图像

时间:2017-02-10 11:41:02

标签: java android android-studio

  

这是MainActivity代码

public class MainActivity extends AppCompatActivity {

    ImageView imageView;
    private static final int START_CAMERA_APP = 1;
    private String imageFileLocation = "";
    private static final int REQUEST_CODE = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView)findViewById(R.id.img_cam);
    }
  

打开相机的方法

 public void takePhoto (View view)
        {
            Intent openCamera = new Intent();
            openCamera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            File photoFile = null;
            try {
                photoFile = createImageFile();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            openCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(openCamera,START_CAMERA_APP);


        }
  

onActivity方法返回捕获的图像

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == START_CAMERA_APP && resultCode == RESULT_OK)
        {
            Bitmap photoCapturedBitmap = BitmapFactory.decodeFile(imageFileLocation);
            imageView.setImageBitmap(photoCapturedBitmap);
        }

        if(requestCode == REQUEST_CODE && resultCode == RESULT_OK)
        {
            Uri uri = null;
            if (data != null)
            {
                uri = data.getData();
                Bitmap bitmap = null;
                try {
                    bitmap = getBitmapFromUri(uri);
                    imageView.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }
  

创建图像的方法

 File createImageFile() throws IOException
    {
        String timeStamp = new SimpleDateFormat("yyyyMMdd+HHmmss").format(new Date());
        String imageFileName = "IMAGE" + timeStamp+ "_";
        File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(imageFileName, ".jpg",storageDirectory);
        imageFileLocation = image.getAbsolutePath();
        return image;
    }
  

最后是与画廊相关的方法       public void gallery(查看视图)       {

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("image/*");
    startActivityForResult(intent,REQUEST_CODE);
}


  private Bitmap getBitmapFromUri (Uri uri) throws IOException
    {
        ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri,"r");
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        parcelFileDescriptor.close();
        return bitmap;
    }

}

1 个答案:

答案 0 :(得分:1)

每次创建新图像时,我都可能会将文件路径保存到共享首选项。然后oncreate检查文件路径的首选项,如果不存在则加载它,如果不能正常继续。

请参阅Shared Preferences