2016-10-04 145 views
1

如何在關閉並重新啓動應用程序後還原從庫中挑選的ImageView。我正在使用SharedPreferences。要在應用程序關閉後保存URI的狀態,但它不起作用,圖像不會再次設置任何幫助,將不勝感激。應用程序關閉並重新啓動後還原的ImageView

public class test extends AppCompatActivity { 

private static int RESULT_LOAD_IMAGE = 1; 
ImageButton buttonLoadImage; 

private static int RESULT_LOAD_IMG = 1; 
String imgDecodableString; 

public String getImagePathFetch; 
ImageView imgView; 

public String keyImage = "myImage"; 
public SharedPreferences sharedPrefEnter, sharedPrefGet; 
Intent galleryIntent; 
Uri selectedImage; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_activity_layout); 

    imgView = (ImageView) findViewById(R.id.imageView1); 

    sharedPrefGet = PreferenceManager.getDefaultSharedPreferences(this); 
    getImagePathFetch = sharedPrefGet.getString(keyImage, ""); 

    if (!getImagePathFetch.equals("")) { 
     // tToast("OnCreate Path=" + getImagePathFetch); 

     Uri uri; 
     uri = Uri.parse(getImagePathFetch); 
     imgView.setImageURI(uri); 
    } 

    buttonLoadImage = (ImageButton) findViewById(R.id.buttonLoadPicture); 
    buttonLoadImage.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 

      AddImage(view); 

     } 
    }); 

} 

public void AddImage(View view) { 

    galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    // Start the Intent 
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG); 
} 

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

    // When an Image is picked 
    if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) { 
     // Get the Image from data 

     selectedImage = data.getData(); 

     sharedPrefEnter = PreferenceManager.getDefaultSharedPreferences(this); 
     SharedPreferences.Editor editor = sharedPrefEnter.edit(); 
     editor.putString(keyImage, selectedImage.toString()); 
     editor.commit(); 

     Uri uri = Uri.parse(selectedImage.toString()); 
     imgView.setImageURI(uri); 

    } 

} 

} 
+0

我已經解決了這個問題,更新我的代碼檢查的運行時權限 – Juancho92

回答

0
// get the path of the image 
    sharedPrefGet = PreferenceManager.getDefaultSharedPreferences(this); 
    getImagePathFetch = sharedPrefGet.getString(keyImage, ""); 

    if (!getImagePathFetch.equals("")) { 
     // tToast("OnCreate Path = " + getImagePathFetch); //TESTING 

     // parse path from toString to uri 
     uriImage = Uri.parse(getImagePathFetch); 

     // explicitly check for the permission at runtime since API 23 
     int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); 

     // Permission not granted 
     if (permissionCheck != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 
        WRITE_EXTERNAL_STORAGE); 
     } else // Permission granted 
     { 

      InputStream imageStream = null; 
      try { 
       imageStream = getContentResolver().openInputStream(uriImage); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream); 
      Bitmap imageResized = getResizedBitmap(yourSelectedImage, 480, 480); // resized 
                        // Image 
      imgView.setImageBitmap(imageResized); 

     } // end if 

    } 

//檢查許可結果。 @覆蓋 公共無效onRequestPermissionsResult(INT requestCode,@NonNull的String []的權限, @NonNull INT [] grantResults){ 開關(requestCode){

case WRITE_EXTERNAL_STORAGE: 
     if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) { 
      InputStream imageStream = null; 
      try { 
       imageStream = getContentResolver().openInputStream(uriImage); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream); 
      Bitmap imageResized = getResizedBitmap(yourSelectedImage, 480, 480); // resized 
                        // Image 
      imgView.setImageBitmap(imageResized); 
     } 
     break; 

    default: 
     break; 
    } 
} 
相關問題