簡體   English   中英

從uri android加載圖片

[英]Loading image from uri android

我目前正在嘗試將已保存到MySQLite數據庫中的圖像的Uri加載到RecyclerView中。

RecyclerViewAdapter的代碼:

public void onBindViewHolder(MyCatchesAdapter.MyViewHolder holder, int position) {
    MyCatch myCatch = myCatchArrayList.get(position);
    holder.txtName.setText(myCatch.get_name());
    holder.txtLocation.setText(myCatch.get_location());
    holder.txtDate.setText(myCatch.get_date());

    holder.catchImage.setImageURI(Uri.parse(myCatch.get_catchImage()));
}

除圖像外,所有數據都加載到回收器視圖中。 我從Logcat獲得以下內容

Unable to open content: content://com.android.externalstorage.documents/document/primary%3ADownload%2Fsunrise.png

java.lang.SecurityException: Permission Denial: opening provider com.android.externalstorage.ExternalStorageProvider from ProcessRecord
{ee1a504 28074:com.example.pavlos.myproject/u0a74} (pid=28074, uid=10074) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS

我的清單權限是:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>

有什么東西我不見了嗎?

編輯:我如何得到Uri:

catchImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,
                    "Select Picture"), SELECT_PICTURE);
        }
    });

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            catchImage.setImageURI(selectedImageUri);
            imagePath = selectedImageUri;
            Log.d("imagePath","URI "+selectedImageUri);
        }
    }
}
public String getPath(Uri uri) {
    // just some safety built in
    if( uri == null ) {
        // TODO perform some logging or show user feedback
        return null;
    }
    // try to retrieve the image from the media store first
    // this will only work for images selected from gallery
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if( cursor != null ){
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(column_index);
        cursor.close();
        return path;
    }
    // this is our fallback here
    return uri.getPath();
}

首先, getPath()完全錯誤。 充其量,它適用於從MediaStore返回的某些圖像子集。 完全擺脫它。 使用圖像加載庫填充ImageView

就您的問題而言,只要該活動實例存在,您的活動就可以使用從ACTION_GET_CONTENT返回的Uri 當且僅當您包含FLAG_GRANT_READ_URI_PERMISSION ,您可以將該Uri傳遞給其他組件(例如,另一個活動)。 您的流程終止后,您將失去訪問該內容的所有權利。 因此,停止將Uri持久保存到數據庫,或切換到ACTION_OPEN_DOCUMENT並獲取可持久的Uri權限。

看起來您正在獲取圖像URI(通常來自圖庫),它將作為內容URI(content://)。 此內容URI帶有臨時權限,當它丟失當前上下文時會丟失。 因此,下次加載URI時,您可能無權在當前上下文中加載它。

我建議你將圖像的內容存儲在DB中,並在需要時加載它。 可以應用諸如圖像壓縮的各種圖像優化。 您也可以將此Glide庫用於Google推薦的圖像管理。

暫無
暫無

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

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