无法解码流:java.io.FileNotFoundException(权限被拒绝) [英] Unable to decode stream: java.io.FileNotFoundException (Permission denied)

查看:149
本文介绍了无法解码流:java.io.FileNotFoundException(权限被拒绝)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一次从图库中上传多个图像.在这里给出.我面临的问题是将字符串转换为位图.我收到以下错误.

I am trying to upload multiple images from gallery at once. given here. The issue i am facing is converting the string to bitmap. I got the following error.

E/BitmapFactory:无法解码流:java.io.FileNotFoundException:/storage/emulated/0/DCIM/Screenshots/Screenshot_2018-11-05-14-59-33-023_com.android.mms.png(权限拒绝)

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Screenshots/Screenshot_2018-11-05-14-59-33-023_com.android.mms.png (Permission denied)

此处发生错误.

位图bitmap2 = PhotoLoader.init().from(imagePath).requestSize(512,512).getBitmap();

Bitmap bitmap2 = PhotoLoader.init().from(imagePath).requestSize(512, 512).getBitmap();

最终字符串encodingString = ImageBase64.encode(bitmap2);//获取异常

final String encodedString = ImageBase64.encode(bitmap2); // getting the exception

但是在我的Log.e中,imagepath中有值,但bitmap2中却没有值.

but in my Log.e there is value in imagepath but cant get value in bitmap2.

用于将图像上传到服务器的按钮代码如下.

 buttonmultiUpload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           Toast.makeText(getApplicationContext(), "Upload button is clicked" + imagesEncodedList, Toast.LENGTH_LONG).show();
            Log.e("list","imagesEncodedList" + imagesEncodedList );
            for( String imagePath: imagesEncodedList){
                try {

                    Log.e("imagePath","imagePath" + imagePath );

                   Bitmap bitmap2 = PhotoLoader.init().from(imagePath).requestSize(512, 512).getBitmap();
                   final String encodedString = ImageBase64.encode(bitmap2);
                    Log.e("bitmap","bitmap" + bitmap2 );
                    String url = "http://myserver.com/imageuploadtest/upload.php";
                    StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                          Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
                          //  gvGallery.setAdapter(null);
                            new AlertDialog.Builder(MainActivity.this).setTitle("Succesful")
                                    .setMessage("Multiple Prescription of Outdoor has been submitted")
                                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                        }
                                    }).show();

                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(getApplicationContext(), "Error while uploading image", Toast.LENGTH_SHORT).show();
                        }
                    }){
                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> params = new HashMap<>();

                            params.put("image", encodedString);
                            params.put("empcode", emp_code);
                            return params;
                        }
                    };
                    myCommand.add(stringRequest);

                } catch (FileNotFoundException e) {
                    Toast.makeText(getApplicationContext(), "Error while loading image", Toast.LENGTH_SHORT).show();
                }
            }

            myCommand.execute();

        }
    });

活动结果代码

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
{


        Toast.makeText(getApplicationContext(), "Clicked for multiple image", Toast.LENGTH_LONG).show();

        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        imagesEncodedList = new ArrayList<String>();
        if(data.getData()!=null){

            Uri mImageUri=data.getData();

            try {
bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(mImageUri));
                   bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), mImageUri);

            } catch (IOException e) {
                e.printStackTrace();
            }

            setToImageView(getResizedBitmap(bitmap, 2048));

            Cursor cursor = getContentResolver().query(mImageUri, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imageEncoded  = cursor.getString(columnIndex);
            cursor.close();

            ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
            mArrayUri.add(mImageUri);

            galleryAdapter = new GalleryAdapter(getApplicationContext(),mArrayUri);
            gvGallery.setAdapter(galleryAdapter);
            gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
            ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery.getLayoutParams();
            mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);

        } else {
            if (data.getClipData() != null) {
                ClipData mClipData = data.getClipData();
                ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
                for (int i = 0; i < mClipData.getItemCount(); i++) {

                    ClipData.Item item = mClipData.getItemAt(i);
                    Uri uri = item.getUri();
                    mArrayUri.add(uri);

                    Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    imageEncoded  = cursor.getString(columnIndex);
                    imagesEncodedList.add(imageEncoded);
                    cursor.close();

                    galleryAdapter = new GalleryAdapter(getApplicationContext(),mArrayUri);
                    gvGallery.setAdapter(galleryAdapter);
                    gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
                    ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery.getLayoutParams();
                    mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);

                }
                Log.v("LOG_TAG", "Selectedmages  " + mArrayUri.size());
                Log.v("LOG_TAG", "Selectedmages  " +  imagesEncodedList );
                Log.v("LOG_TAG", "Selectedmages  " +  bitmap );
            }
        }
    }

推荐答案

将这两个添加到 Mainfest.xml 文件

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

然后您需要先请求权限,然后才能像下面这样动态地进行任何操作

Then you need to request permission before any operation dynamically like below

if (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
    // Permission is not granted
     if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.
    } else {
        // No explanation needed; request the permission
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
    }
}

这篇关于无法解码流:java.io.FileNotFoundException(权限被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆