当前位置: 首页 > 知识库问答 >
问题:

无法解码流:java.io.FileNotFoundException:没有这样的文件或目录)

微生毅
2023-03-14

不幸的是,什么也没有发生,只是这样:无法解码流:java.io.FileNotFoundException:/com.marijan.red.messageActivity@dac48b3/document/image:256(没有这样的文件或目录)

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GALLERY_PICK && resultCode == RESULT_OK){
           Uri imageUri = data.getData();

            DatabaseReference user_message_push = reference.child("Chats")
                    .child(fuser.getUid()).child(userid).push();

            final String push_id = user_message_push.getKey();

            final StorageReference filePath = mImageStorage.child("message_images").child(push_id +"jpg");

            File actualImage = new File (imageUri.getPath());
            try {
                Bitmap compressedImage = new Compressor(this)
                        .setQuality(50)
                        .compressToBitmap(actualImage);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                compressedImage.compress(CompressFormat.JPEG, 50, baos);
                byte[] final_image = baos.toByteArray();

                UploadTask uploadTask = filePath.putBytes(final_image);

                Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                    @Override
                    public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                        if (!task.isSuccessful()) {
                            throw task.getException();
                        }


                        return filePath.getDownloadUrl();
                    }
                }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                    @Override
                    public void onComplete(@NonNull Task<Uri> task) {
                        if (task.isSuccessful()) {

                            String download_url = task.getResult().toString();

                            HashMap<String, Object> hashMap = new HashMap<>();
                            hashMap.put("sender", fuser.getUid());
                            hashMap.put("receiver",userid );
                            hashMap.put("message", download_url);
                            hashMap.put("isseen", false);
                            hashMap.put("type","image");

                            reference.push().setValue(hashMap);

我假设我没有正确地通过图像

共有1个答案

胥玮
2023-03-14

您必须使用文件提供程序[您可以从这里获得知识]。或者您可以从URI中获取位图,并且您必须使用内容解析器,如下所示

位图位图=mediastore.images.media.GetBitmap(getContentResolver(),uri);

所以现在你可以在上传之前压缩你的位图了。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
             path = getImageRealPath(getContentResolver(), uri, null);
  } else {
      path = uri.getPath();
   }

private String getImageRealPath(ContentResolver contentResolver, Uri uri, String whereClause) {
        String ret = "";

        // Query the uri with condition.
        Cursor cursor = contentResolver.query(uri, null, whereClause, null, null);

        if (cursor != null) {
            boolean moveToFirst = cursor.moveToFirst();
            if (moveToFirst) {

                // Get columns name by uri type.
                String columnName = MediaStore.Images.Media.DATA;
                Log.d("UriTest: ", "uri: " + uri);
                if (uri == MediaStore.Images.Media.EXTERNAL_CONTENT_URI) {
                    columnName = MediaStore.Images.Media.DATA;
                }


                // Get column index.
                int imageColumnIndex = cursor.getColumnIndex(columnName);

                // Get column value which is the uri related file local path.
                ret = cursor.getString(imageColumnIndex);
            }
        }

        return ret;
    }
 类似资料: