簡體   English   中英

獲取圖像路徑android

[英]Get image path android

我想將圖像加載到Android上的ImageView中。 現在,我知道如果圖像太大,則必須對其進行縮放,以免占用過多內存。

我想做的就是這里所說的: http : //developer.android.com/training/displaying-bitmaps/load-bitmap.html

現在,我獲取圖像的方式是通過URI這樣的:

Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"text"),1);


protected void onActivityResult(int reqCode, int resCode, Intent data){
    super.onActivityResult(reqCode, resCode, data);

    if (reqCode == 1 && resCode == RESULT_OK && data != null) {
        Uri selectedImage = data.getData();
    }
}

而且,如果我想使用一些encode *()方法,則無法使用URI做到這一點。 所以我的第一選擇是使用:

decodeFile(imageUri.getPath(),options);

但是當我這樣做時,我從圖庫中獲得的路徑是這樣的:/ documents / image:9023

我應該怎么做? 我只需要打開圖庫並從顯示中獲取圖像,也可以將其另存為位圖,以便我可以對其進行編輯。

就連幾天前我也遇到類似的問題,

1.)首先將您的Uri轉換為字符串

String SelectedImageString= selectedImage.toString();

2)一旦獲得圖像的字符串路徑,就可以將其轉換為位圖,並使用bitmapFactory選項調整其大小。

final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8; 
        Bitmap bitmap;
        bitmap=BitmapFactory.decodeFile(SelectedImageString, options);
        imageView.setImageBitmap(bitmap);

它為我工作,希望對您有幫助。

如果我想使用一些decode *()方法,則無法使用URI

你當然可以。 ContentResolver上調用openInputStream() ,然后將該InputStream傳遞給BitmapFactory上的decodeStream()方法。

Uri不一定是文件 ,因此請勿嘗試將其視為一個文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM