【问题标题】:How retrieve image from gallery?如何从图库中检索图像?
【发布时间】:2016-04-02 08:37:10
【问题描述】:

我正在尝试从 Android 的图库中检索图像,但不幸的是,它在获得许可后失败了。

这是我的代码:

public void loadImagefromGallery(View view) {
    // Create intent to Open Image applications like Gallery, Google Photos
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Start the Intent
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                && null != data) {
            // Get the Image from data

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            // Get the cursor
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            Toast.makeText(this, "Image picked" +
                            columnIndex,
                    Toast.LENGTH_LONG).show();

        } else {
            Toast.makeText(this, "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                .show();
        Log.d("ERROR",e.getMessage());
    }

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.melas.app_3" >

   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
   <uses-permission android:name="android.permission.CALL_PHONE" />
   <uses-permission android:name="android.permission.READ_PHONE_STATE" />

   <application
     android:name=".StartApps"
     android:allowBackup="true"
     android:icon="@mipmap/ic_launcher"
     android:label="@string/app_name"
     android:supportsRtl="true"
     android:theme="@style/Theme.AppCompat.NoActionBar" >
<meta-data
    android:name="com.parse.APPLICATION_ID"
    android:value="" />
<meta-data
    android:name="com.parse.CLIENT_KEY"
    android:value="" />

<activity
    android:name=".MainActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".Register" >
</activity>
<activity android:name=".Register2" >
</activity>
<activity android:name=".LoginActivity" >
</activity>

错误:

12-28 15:15:07.100 1847-1859/android.process.media E/DatabaseUtils: Writing exception to parcel
12-28 15:15:07.100 1847-1859/android.process.media E/DatabaseUtils: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/28 from pid=13141, uid=10060 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
12-28 15:15:07.100 1847-1859/android.process.media E/DatabaseUtils:     at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:605)
12-28 15:15:07.100 1847-1859/android.process.media E/DatabaseUtils:     at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:480)
12-28 15:15:07.100 1847-1859/android.process.media E/DatabaseUtils:     at android.content.ContentProvider$Transport.query(ContentProvider.java:211)
12-28 15:15:07.100 1847-1859/android.process.media E/DatabaseUtils:     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
12-28 15:15:07.100 1847-1859/android.process.media E/DatabaseUtils:     at android.os.Binder.execTransact(Binder.java:453)

【问题讨论】:

标签: java android gallery


【解决方案1】:

整个AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.melas.app_3" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:name=".StartApps"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.NoActionBar" >
    <meta-data
        android:name="com.parse.APPLICATION_ID"
        android:value="" />
    <meta-data
        android:name="com.parse.CLIENT_KEY"
        android:value="" />

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Register" >
    </activity>
    <activity android:name=".Register2" >
    </activity>
    <activity android:name=".LoginActivity" >
    </activity>
</application>

【讨论】:

    【解决方案2】:

    我建议你放弃存储权限请求并使用ACTION_GET_CONTENT

    用法是这样的:

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivity(Intent.createChooser(intent, "Select image"));
    

    这种方式非常棒,因为您的应用根本没有任何存储权限。仅当您的目标 API maxSdkVersion=18 来请求权限,只有在别无他法的情况下。

    【讨论】:

    • 然后呢?如何检索选定的图像?
    • 我找到了!谢谢大家!
    • 我有一个重要的问题.....我是 android dev 的新手......我想知道在哪里以及如何启动我的应用程序......我正在做一个新的应用程序parse.com....是正确的方法吗?你能建议吗?我们将不胜感激。
    【解决方案3】:

    它有效,但我尝试检索文件名以进行解析....

    public void loadImagefromGallery(View view) {
         Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        // Start the Intent
        startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
    
    
    }
     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data); 
    txttest = (TextView)findViewById(R.id.textViewtest);
            ImageView ig=(ImageView)findViewById(R.id.imageViewTest);
    
    
          try {
                // When an Image is picked
                if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                        && null != data) {
                    // Get the Image from data
    
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };
    
                    // Get the cursor
                    Cursor cursor = getContentResolver().query(selectedImage,
                            filePathColumn, null, null, null);
                    // Move to first row
                    cursor.moveToFirst();
    
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    
                    Bitmap bitmap = BitmapFactory.decodeFile(selectedImage.toString());
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] image = stream.toByteArray();
                    Glide.with(this)
                            .load(selectedImage)
                            .into(ig);
    
                    Toast.makeText(this, "Image picked" +
                                   selectedImage,
                            Toast.LENGTH_LONG).show();
    
    //txttest.setText();
                    } else {
                        Toast.makeText(this, "You haven't picked Image",
                                Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                            .show();
                    Log.d("ERROR", e.getMessage());
                }
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-25
      相关资源
      最近更新 更多