그래서 사용자가 선택한 이미지에서 이미지 경로를 저장하여 사용자가 앱을 종료하고 다시 열 때 이미지가 여전히 아이콘에 대한 배경 또는 imageView로 설정됩니다 (둘 다 동일한 이미지 경로 메서드를 사용함).
다음은 제 코딩입니다 (Drag_and_Drop_App.java).
public Bitmap getThumbnail(String filename) {
Bitmap thumbnail = null;
try {
File filePath = this.getFileStreamPath(filename);
FileInputStream fi = new FileInputStream(filePath);
thumbnail = BitmapFactory.decodeStream(fi);
} catch (Exception ex) {
Log.e("getThumbnail() on internal storage", ex.getMessage());
}
return thumbnail;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("Drag_and_Drop_App", "requestCode: " + requestCode + ", resultCode: " + resultCode);
if(requestCode == SET_BACKGROUND && resultCode == RESULT_OK){
byte[] byteArray = data.getByteArrayExtra("myBackgroundBitmap");
Bitmap myBackground = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
setBackgroundImage(myBackground);
}
else if(requestCode == RESULT_ICON){
byte[] byteArray = data.getByteArrayExtra("myIconBitmap");
Bitmap myIcon = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
setBackgroundImageForIcon(myIcon);
Log.d("Drag_and_Drop_App", "Icon is set");
}
}
Personalize.java :
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String imagePath = cursor.getString(column_index);
if(cursor != null) {
cursor.close();
}
return imagePath;
}
private void setIconImageInWidget() {
// TODO Auto-generated method stub
Log.d("Personalize", "setIconImageInWidget() called");
Intent i = getIntent();
//Convert bitmap to byte array to send back to activity
// See: http://stackoverflow.com/questions/11010386/send-bitmap-using-intent-android
scaleDownBitmapForIcon(b2, 500, this.getBaseContext());
Log.d("Personalize", "Scale Bitmap Chosen For Icon");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b2.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[]byteArray = stream.toByteArray();
i.putExtra("myIconBitmap", byteArray);
setResult(RESULT_ICON, i);
finish();
}
private void setBackgroundImageInDragAndDrop() {
Log.d("Personalize", "setBackgroundImageInDragAndDrop() called");
Intent i = getIntent();
//Convert bitmap to byte array to send back to activity
// See: http://stackoverflow.com/questions/11010386/send-bitmap-using-intent-android
scaleDownBitmap(background, 500, this.getBaseContext());
Log.d("Personalize", "Scale Bitmap Chosen");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
background.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[]byteArray = stream.toByteArray();
i.putExtra("myBackgroundBitmap", byteArray);
setResult(RESULT_OK, i);
finish();
}
@Override
protected void onPause() {
SharedPreferences sp = getSharedPreferences("AppSharedPref", 1); //open shared preferences with name AppSharedPref
Editor editor = sp.edit();
editor.putString("ImagePath", selectedImagePath); //Store selectedImagePath with key "ImagePath". This key will be then used to retrieve data.
editor.commit();
super.onPause();
Log.d("Personalize", "onPause() called and selectedImagePath saved");
}
@Override
protected void onResume() {
SharedPreferences sp = getSharedPreferences("AppSharedPref", 1);
selectedImagePath = sp.getString("ImagePath", "");
super.onResume();
Log.d("Personalize", "onResume() called and images uploaded");
}
public boolean saveImageToInternalStorage(Bitmap image) {
try {
FileOutputStream fos = this.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
return true;
} catch (Exception e) {
return false;
}
}
문제는 이것이 작동하지 않고 앱을 다시 열 때 배경이 이전에 선택한 이미지로 설정되지 않는다는 것입니다.
sharedpref onResume ()에서 이미지 경로를 가져올 때 selectedPath가 null이 아닌 경우 해당 이미지를 배경으로 추가하십시오.
@Override
protected void onResume() {
SharedPreferences sp = getSharedPreferences("AppSharedPref", 1);
selectedImagePath = sp.getString("ImagePath", "");
super.onResume();
Log.d("Personalize", "onResume() called and images uploaded");
Log.d("Personalize", "Now set the image as background");
background = getAndDecodeImage(selectedImagePath);
if(background != null){
image.setImageBitmap(background);
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다