2015-07-11 98 views
0

我有一個onclick,這將允許用戶從圖庫中選擇像這樣一個文件:安卓:保存從庫圖像,然後加載到ImageView的

case R.id.ColourCustom: 
       customBorderChange(); 
       break; 

private void customBorderChange() { 
    final ImageView QPBackground = (ImageView) findViewById(R.id.QPBackground); 
    menuHandler.removeCallbacks(menuTimer); 
    menuHandler.postDelayed(menuTimer, 5000); 
    bgHandler.removeCallbacks(runnableBG); 
    bgHandler.postDelayed(runnableBG, 2000); 
    Intent i = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    startActivityForResult(i, GALLERY_REQUEST); 
    File file = getFileStreamPath("QuickPlayBG.png"); 
    if (file.exists()) { 
     QPBackground.setImageBitmap(getThumbnail("QuickPlayBG.png")); 
    } else { 
     String uri21 = "@drawable/bg_green"; 
     int imageResource21 = getResources().getIdentifier(uri21, null, getPackageName()); 
     QPBackground.setBackgroundResource(imageResource21); 
    } 
} 

這在這裏向您發送:

protected void onActivityResult (int requestCode, int resultCode, Intent data) 
{ 
    Uri selectedImageUri; 
    if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK && null != 
      data) { 
     selectedImageUri = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

     Cursor cursor = getContentResolver().query(selectedImageUri, 
       filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     cursor.close(); 
    } 
    try { 
     Bundle extras = data.getExtras(); 
     Bitmap photo = extras.getParcelable("data"); 
     saveBGFile(photo); 
    } catch (Exception e) { 
     String errorMessage = "Make your mind up mate!"; 
     Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
    try { 
     saveQPConfig(); 
    } catch (IOException e) { 
     String errorMessage = "Saving failed"; 
     Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
} 

節省了文件,使用saveBGFile:

public void saveBGFile(Bitmap image) { 
    FileOutputStream out = null; 
    String filename = "QuickPlayBG.png"; 
    try { 
     out = new FileOutputStream(filename); 
     image.compress(Bitmap.CompressFormat.PNG, 100, out); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      if (out != null) { 
       out.close(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

的問題是,只有這一行:

QPBackground.setImageBitmap(getThumbnail("QuickPlayBG.png")); 

它不加載任何東西。如果我將「QuickPlayBG.png」更改爲我在另一部分應用中使用的另一個文件名,則加載正常。這兩個文件都是使用相同的方法創建的。我證實存在「QuickPlayBG.png」。

編譯器給了我下面的提示:

E/﹕ ReadStreamToBuffer : Qmage Stream read error!! required length 12 bytes, but red 0 bytes 
E/﹕ isQmage : input SkStreamRewindable length is less than Qmage minimum size : 0 

我認爲這與我保存圖像的方式做,但我看不到自己的過錯。可能是什麼問題,它不是加載圖像?

編輯:

這裏是getThumbnail方法我使用(適用於另一個文件名):

public Bitmap getThumbnail(String filename) { 
    final Context context = this; 
    String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath(); 
    Bitmap thumbnail = null; 

    if (thumbnail == null) { 
     try { 
      File filePath = context.getFileStreamPath(filename); 
      FileInputStream fi = new FileInputStream(filePath); 
      thumbnail = BitmapFactory.decodeStream(fi); 
     } catch (Exception ex) { 
      Log.e("getThumbnail() !exist", ex.getMessage()); 
     } 
    } 
    return thumbnail; 
} 

回答

0

使用這個這個簡短而親切的代碼。使用圖庫的意圖。

1.declaire變量。

private static int RESULT_IMG = 1; 
String imgString; 

2.在onclick按鈕上調用圖庫的意圖。

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

3.onActivityResult to your code。

@Override 
protected void onActivityResult(int requestCode, int responseCode, Intent data) { 
super.onActivityResult(requestCode, responseCode, data); 
try { 
    if (requestCode == RESULT_IMG && responseCode == RESULT_OK 
        && null != data) { 
    Uri pickedImage = data.getData(); 
    String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = getContentResolver().query(pickedImage, 
      filePathColumn, null, null, null); 
    cursor.moveToFirst(); 
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
    imgString = cursor.getString(columnIndex); 
    cursor.close(); 

    //set bitmap to your imageview 
    ImageView imgView = (ImageView) findViewById(R.id.imgView); 
    imgView.setImageBitmap(BitmapFactory.decodeFile(imgString)); 
} else { 
    Toast.makeText(this, "please select picture",Toast.LENGTH_LONG).show(); 
} 
} catch (Exception e) { 
    Toast.makeText(this, "error message", Toast.LENGTH_LONG) 
        .show(); 
    } 
}