Android 无法解码流:java.io.FileNotFoundException(权限被拒绝)

Android 无法解码流:java.io.FileNotFoundException(权限被拒绝),android,android-bitmap,Android,Android Bitmap,我试图一次从图库上传多张图片。给这里。我面临的问题是将字符串转换为位图。我犯了以下错误 E/BitmapFactory:无法解码流:java.io.FileNotFoundException:/storage/emulated/0/DCIM/Screenshots/Screenshot_2018-11-05-14-59-33-023_com.android.mms.png(权限被拒绝) 错误发生在此处。 buttonmultiUpload.setOnClickListener(new View

我试图一次从图库上传多张图片。给这里。我面临的问题是将字符串转换为位图。我犯了以下错误

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

错误发生在此处。

 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();

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

最终字符串encodedString=ImageBase64.encode(位图2);//获取异常

但在我的Log.e中,imagepath中有值,但无法在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();

        }
    });
buttonmultiUpload.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Toast.makeText(getApplicationContext(),“单击上载按钮”+imagesEncodedList,Toast.LENGTH_LONG).show();
Log.e(“列表”、“imagesEncodedList”+imagesEncodedList);
用于(字符串imagePath:imagesEncodedList){
试一试{
Log.e(“imagePath”、“imagePath”+imagePath);
位图bitmap2=PhotoLoader.init().from(imagePath).requestSize(512,512).getBitmap();
最终字符串encodedString=ImageBase64.encode(位图2);
Log.e(“位图”、“位图”+位图2);
字符串url=”http://myserver.com/imageuploadtest/upload.php";
StringRequest StringRequest=new StringRequest(Request.Method.POST,url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
Toast.makeText(getApplicationContext(),response,Toast.LENGTH_SHORT.show();
//gvGallery.setAdapter(空);
新建AlertDialog.Builder(MainActivity.this).setTitle(“成功”)
.setMessage(“已提交多个户外处方”)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
}
}).show();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(getApplicationContext(),“上载图像时出错”,Toast.LENGTH_SHORT.show();
}
}){
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
参数put(“图像”,编码字符串);
参数put(“emp代码”,emp_代码);
返回参数;
}
};
myCommand.add(stringRequest);
}catch(filenotfounde异常){
Toast.makeText(getApplicationContext(),“加载图像时出错”,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 );
            }
        }
    }
activityresult上受保护的void(int-requestCode、int-resultCode、Intent-data){
super.onActivityResult(请求代码、结果代码、数据);
{
Toast.makeText(getApplicationContext(),“单击可获得多个图像”,Toast.LENGTH_LONG.show();
字符串[]filePathColumn={MediaStore.Images.Media.DATA};
imagesEncodedList=新的ArrayList();
if(data.getData()!=null){
Uri mimageri=data.getData();
试一试{
位图=BitmapFactory.decodeStream(getContentResolver().openInputStream(Mimageri));
位图=MediaStore.Images.Media.getBitmap(getContentResolver(),mimageri);
}捕获(IOE异常){
e、 printStackTrace();
}
setToImageView(getResizedBitmap(位图,2048));
Cursor Cursor=getContentResolver().query(mimageri,filePathColumn,null,null);
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(filePathColumn[0]);
imageEncoded=cursor.getString(columnIndex);
cursor.close();
ArrayList mArrayUri=新的ArrayList();
mArrayUri.add(mImageUri);
galleryAdapter=新galleryAdapter(getApplicationContext(),mArrayUri);
gvGallery.setAdapter(galleryAdapter);
gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
ViewGroup.MarginLayoutParams mlp=(ViewGroup.MarginLayoutParams)gvGallery.getLayoutParams();
setMargins(0,gvGallery.getHorizontalSpacing(),0,0);
}否则{
if(data.getClipData()!=null){
ClipData mClipData=data.getClipData();
ArrayList mArrayUri=新的ArrayList();
对于(int i=0;i@Override
public void onRequestPermissionsResult(int requestCode,
    String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1:
            {
                if (grantResults.length > 0 &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission was granted, do something you want        
                } else {
                    // permission denied
                    Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
                }
                return;
            }
    }
}
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
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);
    }
}