Android应用无法在我的图库中编写

时间:2016-06-10 14:46:10

标签: android

我的所有人应该使用uploadlicence按钮从我的画廊中选择图片,然后使用插入按钮将其插入网络服务器。

选择pic没有问题。单击插入时出现以下错误。 (我检查了选择过程,并且它正在运行)

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/100ANDRO/DSC_0042.JPG: open failed: EACCES (Permission denied).

我已经为我的ManiFest文件添加了所需的权限,如下所示,但问题仍然存在。

编辑:(已解决):在Marchmellow中,您需要以编程方式检查权限并将其授予用户,我在下面做了:

public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

then add the function before the operation you want:

verifyStoragePermissions(YourActivity.this);

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rentacar.rentacar">

<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.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />

<application

我的代码:

UploadLicence.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            // Start the Intent
            startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
        }
    });

InsertRentCar.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           if (imgPath != null && !imgPath.isEmpty()) {

               // Convert image to String using Base64
               encodeImagetoString();
               // When Image is not selected from Gallery
           } else {
               Toast.makeText(
                       getApplicationContext(),
                       "You must select image from gallery before you try to upload",
                       Toast.LENGTH_LONG).show();
           }
       }
   });

 @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]);
            imgPath = cursor.getString(columnIndex);
            cursor.close();
            // Get the Image's file name
            String fileNameSegments[] = imgPath.split("/");
            fileName = fileNameSegments[fileNameSegments.length - 1];
            // Put file name in Async Http Post Param which will used in Php web app
            Toast.makeText(getApplicationContext(), imgPath, Toast.LENGTH_LONG).show();
            Toast.makeText(getApplicationContext(), fileName, 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();
    }
}

 public void encodeImagetoString() {new AsyncTask<Void, Void, String>() {
    protected void onPreExecute() {
    }
    @Override
    protected String doInBackground(Void... params) {

        BitmapFactory.Options options = null;
        options = new BitmapFactory.Options();
        options.inSampleSize = 3;
        bitmap = BitmapFactory.decodeFile(imgPath,
                options);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        // Must compress the Image to reduce image size to make upload easy
        bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
        byte[] byte_arr = stream.toByteArray();
        // Encode Image to String
        encodedString = Base64.encodeToString(byte_arr, 0);
        return "";
    }

    @Override
    protected void onPostExecute(String msg) {
        // Put converted Image string into Async Http Post param
        // Trigger Image upload
        triggerImageUpload();
    }
}.execute(null, null, null);

}

public void triggerImageUpload() {
    Toast.makeText(getApplicationContext(), encodedString, Toast.LENGTH_LONG).show();
}

0 个答案:

没有答案