这是 MainActivity.java。应用程序包含一个带有ImageView和Button的活动。点按这些按钮时,图库必须打开。之后,您将检查粘贴在ImageView中的一些图像。想要退出应用程序或点按时,我无法将图像保存在活动中。
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
static final int GALLERY_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
Bitmap bitmap = null;
ImageView imageView = (ImageView) findViewById(R.id.imageView);
switch(requestCode) {
case GALLERY_REQUEST:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
super.onResume();
}
}
}}
这是 content_main.xml。这里我创建了一个按钮和空ImageView,用于粘贴图库中的图像。请告诉我们如何在活动中保存图像和文字。
答案 0 :(得分:0)
当活动关闭时,它将被销毁。您应该将数据保存在某处,然后将其还原。例如,在SharedPreferences中。最简单的方法如下所示。
public class MainActivity extends AppCompatActivity {
static final int GALLERY_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
}
});
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
String uriString = preferences.getString("MainPicture", null);
if (uriString != null) {
Bitmap bitmap = null;
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Uri selectedImage = Uri.parse(uriString);
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("MainPicture", selectedImage.toString());
editor.apply();
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
Bitmap bitmap = null;
ImageView imageView = (ImageView) findViewById(R.id.imageView);
switch(requestCode) {
case GALLERY_REQUEST:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("MainPicture", selectedImage.toString());
editor.apply();
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
super.onResume();
}
}
}}