2013-07-31 66 views
0

我試圖將從圖庫加載的圖像保存到手機內存(本地路徑)。任何人都可以引導我進入這個?將圖像保存到從圖庫加載的手機

這是我從庫中獲取圖像。

ImageView profilePicture; 
private Uri imageUri; 
String picturePath; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    profilePicture = (ImageView) findViewById(R.id.profile_picture); 
     profilePicture.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View arg0, MotionEvent arg1) { 
       switch (arg1.getAction()) { 
       case MotionEvent.ACTION_DOWN: { 
        break; 
       } 
       case MotionEvent.ACTION_UP:{ 
        uploadImage(); 
        break; 
       } 
       } 
       return true; 
      } 
     }); 
} 

uploadImage()

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

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     switch (requestCode) { 
     case 0: 
      if (resultCode == Activity.RESULT_OK) { 
       Uri selectedImage = imageUri; 
       getContentResolver().notifyChange(selectedImage, null); 
       ContentResolver cr = getContentResolver(); 
       Bitmap bitmap; 
       try { 
        bitmap = android.provider.MediaStore.Images.Media 
        .getBitmap(cr, selectedImage); 

        profilePicture.setImageBitmap(bitmap); 
       } catch (Exception e) { 
        Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT) 
          .show(); 
        Log.e("Camera", e.toString()); 
       } 
      } 
     case 1: 
      if (resultCode == Activity.RESULT_OK && null != data) { 
       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]); 
       picturePath = cursor.getString(columnIndex); 
       cursor.close(); 
       profilePicture.setBackgroundColor(Color.TRANSPARENT); 
        profilePicture.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

      } 

     } 
    } 

*注:案例0是使用手機攝像頭拍攝。

我可以在我的ImageView顯示,但我需要這些信息存儲在手機的內存中,因此每次我都會打開應用程序,我就能到以前上傳的圖像加載到圖像視圖。然後,如果用戶想要再次上傳。之前保存的文件將被覆蓋。我不想使用sqlite將圖像存儲爲blob,因爲我只會爲我的整個應用上傳一張圖片。我想將它存儲在像myappname/images/image.png這樣的本地文件路徑中。有任何想法嗎?謝謝!

回答

1

您可以在應用程序緩存目錄中的圖像存儲諸如:

try { 
    String destFolder = getCacheDir().getAbsolutePath()+ "/images/"; 
     if (!new File(destFolder).exists()) { 
      new File(destFolder).mkdirs(); 
     } 

     FileOutputStream out = new FileOutputStream(destFolder + "profile.png");  
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 
     out.close(); 
} catch (Exception ex) { 
    ex.printStackTrace();    
} 

並回讀文件到Bitamp:

String fname = "profile.png"; 
Bitmap profile = BitmapFactory.decodeFile(getCacheDir().getAbsolutePath()+ "/images/" + fname); 
+0

我會嘗試。但是,請您指出保存圖像中的哪一部分是我引用當前圖像的那一行?如你看到的。我從圖庫中獲得的圖像直接存儲在imageview中。這條線。 profilePicture.setImageBitmap(BitmapFactory.decodeFile(picturePath)); bitmap.com是否具有我擁有的圖像?所以我應該將BitmapFactory.decodeFile(picturePath)存儲爲一個位圖。 BM。然後在壓縮前使用這個:bm.compress? – ljpv14

+0

這是你的位圖:BitmapFactory.decodeFile(picturePath));所以嘗試設置:BitmapFactory.decodeFile(picturePath))。compress(Bitmap.CompressFormat.PNG,100,out); –

+0

我將它存儲到一個位圖變量,使其更加有組織。 +1美好而簡單的回答。正是我需要的。沒有其他不必要的代碼和解決方法。開門見山。謝謝! – ljpv14

相關問題