改造上传图片,FileNotFoundException,内容类型uri

时间:2016-11-14 11:41:14

标签: android android-studio retrofit retrofit2

尝试使用Retrofit将图像上传为文件,在路径类型为file://时上传文件但现在由于naugat已将路径Uri更改为Content:/类型,现在当我转换此路径时如果要提交FileNotFoundException

,请提交并进行改装调用
RequestBody requestFile =
                RequestBody.create(MediaType.parse("multipart/form-data"),photoFile);

这是上面使用的photoFile的值 - file:/storage/emulated/0/Android/data/com.Bawa.Sketches/files/Pictures/JPEG_20161114_063716_-1561886067.jpg

开发人员网站中提供的setPic()方法

   private void setPic(ImageView sketchIv) {
    InputStream input = null;
    try {
        input = getContentResolver().openInputStream(Uri.parse(mCurrentPhotoPath));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    // Get the dimensions of the View
    int targetW = 300;
    int targetH = 300;

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(input, null, bmOptions);
    try {
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;
    try {
        input = getContentResolver().openInputStream(Uri.parse(mCurrentPhotoPath));

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(input, null, bmOptions);
    sketchIv.setImageBitmap(bitmap);
    compressImage(bitmap);
    //showAlertDialog(bitmap);

}

调用CompressImage()方法来减小图像的大小并在方法中获得photoFile

 private void compressImage(Bitmap lbitmap) {

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
    File f = new File(Uri.parse(mCurrentPhotoPath).toString());
    try {
        f.createNewFile();


        Bitmap bitmap = lbitmap;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 20, bos);
        byte[] bitmapdata = bos.toByteArray();

        FileOutputStream fos = null;

        fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {

    }

    photoFile = f;
    Log.i("response","FILE : "+ f);

}

P.S。已阅读过Commonsware的答案大多数地方 - If you get a content:// Uri, please consume it using a ContentResolver and methods like openInputStream() and openOutputStream().
不知道如何实现这个。

2 个答案:

答案 0 :(得分:1)

  

调用CompressImage()方法来减小图像的大小并在方法中获得photoFile值

We discussed this previously

Uri.parse(mCurrentPhotoPath).toString()

mCurrentPhotoPathparse()返回给您,toString()mCurrentPhotoPath互相撤消。 UricompressImage()的字符串表示形式,而不是文件系统路径。

因此,请修改InputStream以使用您在setPic()中使用的相同http://toodey.com/2015/08/10/hadoop-installation-on-windows-without-cygwin-in-10-mints/ 方法。

答案 1 :(得分:0)

检查此代码,使用图像路径创建一个键入的文件。我认为它会奏效。

TypedFile typedFile = new TypedFile("image/jpeg", new File(photoFile ));

         or

如果您使用的是2以上的改装版本,那么您可以使用以下代码。

RequestBody file = RequestBody.create(MediaType.parse("image/*"), photoFile );