2016-08-29 10 views
0

私の目標は、アプリケーションを閉じた後(破棄して)、最後に選択した画像をImageViewに残すことです。アプリを再入力した後に最後に選択した画像をImageViewに読み込む

Iは、私はSharedPreferencesビットマップ、ピカソを使用して、それをロードするパスを格納しようとし、(How can I store images using sharedpreference in android?)」SharedPreferenceにビットマップを保存するために、ロードしようとした:(Picasso Load image from filesystem)としてBitmapFactory.decodeFile(completePath);に値を渡すことによってそれをロードしようとしましたあなたは私のコードで見ることができる

はとにかく出力が常に同じである

;。私はNullPointerExceptionが取得しています

私は一般的にはAndroidのプログラミングに新しいですので、私の乱雑なコードを許す

private String picturePass; 
private ImageView imageView; 
private Bitmap picBitmap; 
private boolean yes; 
private static final int SELECT_PICTURE = 1; 
private static final String SAVE_NAME = "MyFile";) 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_activity_page); 
    if (savedInstanceState != null) { 
     savedInstanceState.get("path"); 
    } 
//loading 
    SharedPreferences preferences = getSharedPreferences(SAVE_NAME, MODE_PRIVATE); 
    //boolean to check if Image was chosen 
    yes = preferences.getBoolean("yes", true); 
    if(yes){ 
     picturePass = preferences.getString("path", null); 
     String completePath = picturePass; 
     //just to check the content 
     Toast.makeText(this, completePath, Toast.LENGTH_LONG).show(); 
     File file = new File(completePath); 
     if (file.exists()) { 
      picBitmap = BitmapFactory.decodeFile(completePath); 
      imageView.setImageBitmap(picBitmap); 
     } 
    } 
} 
//Choosing a picture from gallery 
public void onClickImage(View view) { 
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    startActivityForResult(intent, SELECT_PICTURE); 
    yes = true; 
} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == SELECT_PICTURE && resultCode == RESULT_OK && null != data) { 
     //The only way I maneged to get the file path, that's a messy part 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
     Cursor cursor = getContentResolver().query(selectedImage, 
       filePathColumn, null, null, null); 
     cursor.moveToFirst(); 
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     cursor.close(); 
     //Setting picture in imageView 
     if (selectedImage != null) { 

      try { 
       Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage); 
       imageView = (ImageView) findViewById(R.id.imageView); 
       imageView.setImageBitmap(bitmap); 
       picBitmap = bitmap; 
       //Tried to use Environment.getExternalStorageDirectory().getAbsolutePath() + imageName with no success 
       //File f = new File(picturePath); 
       //String imageName = f.getName(); 
       //picturePass = imageName; 
       picturePass = picturePath; 
       //Saving image path 
       //just too check the content 
       Toast.makeText(this, picturePath, Toast.LENGTH_LONG).show(); 
       SharedPreferences.Editor editor = getSharedPreferences(SAVE_NAME, MODE_PRIVATE).edit(); 
       editor.putString("path", picturePass); 
       editor.putBoolean("yes", true); 
       editor.commit(); 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } else { 
     super.onActivityResult(requestCode, resultCode, data); 
    } 
} 
@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putString("path", picturePass); 
} 
} 
。 すべての

08-29 19:05:46.263 16436-16436/com.amberapply.vitaliy.iloveyou E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.amberapply.vitaliy.iloveyou, PID: 16436 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amberapply.vitaliy.iloveyou/com.amberapply.vitaliy.iloveyou.MainActivityPage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5258) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:940) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:735) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference 
    at com.amberapply.vitaliy.iloveyou.MainActivityPage.onCreate(MainActivityPage.java:60) 
    at android.app.Activity.performCreate(Activity.java:6005) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)  
    at android.app.ActivityThread.access$800(ActivityThread.java:151)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:135)  
    at android.app.ActivityThread.main(ActivityThread.java:5258)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:372)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:940)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:735)                   
+0

"MainActivityPage.java:60" 'MainActivityPage.java'の60行目は何ですか? –

答えて

1

@Nikunj Sardhara液が

を動作するはず:onCreateに `imageView.setImageBitmap(picBitmap)前にはimageView = (ImageView) findViewById(R.id.imageView);はありません。

+0

ありがとう、それは働いた!あなたがそれを指摘した後、私のレベルでさえ、これは愚かな間違いです。私は質問をする前に、次回より慎重になるでしょう。 。 – Mikhail

1

まずSharedPreferencesで画像を保存することは非常に悪い考えです。 SharedPreferencesに画像のパスを保存する必要があります。初めてアプリを読み込んだときにその画像を読むことができます。SharedPreferencesを読み、画像パスを取得し、ストレージから読み込んでImageViewに表示します。

これは手順であり、簡単です。このコードを書く必要はありません。 今、あなたは本当にあなたが使用することができ、SharedPreferencesにブール値を格納する必要はありませんが含まれています()メソッド:https://developer.android.com/reference/android/content/SharedPreferences.html

と、NullPointerExceptionが発生について、私はlogcatをチェックし、可能な場合は、その後にNullPointerExceptionの時点で、あなたのlogcatをコピーしてくださいと言うでしょう あなたのlogcatをここに投稿すると、もっと役立つでしょう。ここ あなたはlogcatを得ることができる方法は次のとおりです。あなたがNPEを修正する場合https://developer.android.com/studio/debug/am-logcat.html

+0

あなたのリプレイをありがとう!画像をソートするためにSharedPreferencesを使用しないことを覚えています。そして、logcatチュートリアルに感謝しました。それは便利でした。 – Mikhail

関連する問題