Java 您好,我已经编写了这个简单的应用程序,我现在想要的是当我的应用程序重新打开时显示我上次单击的图像
这是主活动代码 方法打开相机 onActivity方法返回捕获的图像 方法创建图像 最后是与画廊相关的方法 公共空间画廊(视图) {Java 您好,我已经编写了这个简单的应用程序,我现在想要的是当我的应用程序重新打开时显示我上次单击的图像,java,android,android-studio,Java,Android,Android Studio,这是主活动代码 方法打开相机 onActivity方法返回捕获的图像 方法创建图像 最后是与画廊相关的方法 公共空间画廊(视图) { }每次创建新图像时,我可能会将文件路径保存到共享首选项。然后oncreate检查文件路径的首选项,如果存在,则加载它,如果不继续,则正常加载 请参见问题出在哪里?到目前为止,您尝试了什么?您已经在存储图像路径imageFileLocation=image.getAbsolutePath()存储此路径,稍后当应用程序启动时,使用此图像路径查看您的图像应用程序可以捕获
}每次创建新图像时,我可能会将文件路径保存到共享首选项。然后oncreate检查文件路径的首选项,如果存在,则加载它,如果不继续,则正常加载
请参见问题出在哪里?到目前为止,您尝试了什么?您已经在存储图像路径
imageFileLocation=image.getAbsolutePath()
存储此路径,稍后当应用程序启动时,使用此图像路径查看您的图像应用程序可以捕获图像并将其保存在设备中。重新启动应用程序后,如何在图像视图中显示上次捕获的图像。我是android初学者,请您具体说明一下。本网站是针对编程问题,而不是代码请求。使用共享首选项(例如)存储img路径,并在下次运行时重新打开它。但是尝试一些东西,一旦失败,在这里发布:)在“onResume”中更好。
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);
}
@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;
}
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;
}