设置图像uri导致无法解码流:java.io.FileNotFoundException: [英] setting image uri gives Unable to decode stream: java.io.FileNotFoundException:
问题描述
我有一个简单的应用程序,可使用以下代码使用Camera捕获图像
I've simple app for capturing image using Camera using following code
@AfterPermissionGranted(RC_STORAGE_PERMS)
private void launchCamera() {
Log.d(TAG, "launchCamera");
// Check that we have permission to read images from external storage.
String perm = android.Manifest.permission.READ_EXTERNAL_STORAGE;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !EasyPermissions.hasPermissions(this, perm)) {
EasyPermissions.requestPermissions(this, getString(R.string.rationale_storage),
RC_STORAGE_PERMS, perm);
return;
}
// Create intent
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Choose file storage location
File file = new File(Environment.getExternalStorageDirectory(), UUID.randomUUID().toString() + ".jpg");
mFileUri = Uri.fromFile(file);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);
// Launch intent
startActivityForResult(takePictureIntent, RC_TAKE_PICTURE);
}
现在我想将该图像上传到Firebase存储
now I want to upload that image to Firebase storage
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult:" + requestCode + ":" + resultCode + ":" + data);
if (requestCode == RC_TAKE_PICTURE) {
if (resultCode == RESULT_OK) {
if (mFileUri != null) {
uploadFromUri(mFileUri);
} else {
Log.w(TAG, "File URI is null");
}
} else {
Toast.makeText(this, "Taking picture failed.", Toast.LENGTH_SHORT).show();
}
}
}
private void uploadFromUri(Uri fileUri) {
Log.d(TAG, "uploadFromUri:src:" + fileUri.toString());
// [START get_child_ref]
// Get a reference to store file at photos/<FILENAME>.jpg
final StorageReference photoRef = mStorageRef.child("photos")
.child(fileUri.getLastPathSegment());
// [END get_child_ref]
// Upload file to Firebase Storage
// [START_EXCLUDE]
showProgressDialog();
// [END_EXCLUDE]
Log.d(TAG, "uploadFromUri:dst:" + photoRef.getPath());
photoRef.putFile(fileUri)
.addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Upload succeeded
Log.d(TAG, "uploadFromUri:onSuccess");
// Get the public download URL
mDownloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
Log.w("IMAGE_URL", "Path is " + mDownloadUrl.toString());
uploadedImage = (ImageView) findViewById(R.id.uploaded_img);
try{// Here I'm setting image in ImageView
uploadedImage.setImageURI(mDownloadUrl);
}catch (Exception e){
System.out.print(e.getCause());
}
// [START_EXCLUDE]
hideProgressDialog();
///updateUI(mAuth.getCurrentUser());
// [END_EXCLUDE]
}
})
);
}
在上线
try{// Here I'm setting image in ImageView
uploadedImage.setImageURI(mDownloadUrl);
}catch (Exception e){
System.out.print(e.getCause());
}
未在ImageView中设置图像并且出现错误
image is not set in ImageView and I get error
07-29 09:54:23.055 18445-18445/? W/IMAGE_URL: Path is https://firebasestorage.googleapis.com/v0/b/connectin-a74da.appspot.com/o/photos%2F7dd3d46f-ed7b-4020-bc89-fd9e19a8ec65.jpg?alt=media&token=5b4f9ad7-1e99-42b8-966d-50c74fc2eab6
07-29 09:54:23.056 18445-18445/? E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: https:/firebasestorage.googleapis.com/v0/b/connectin-a74da.appspot.com/o/photos%2F7dd3d46f-ed7b-4020-bc89-fd9e19a8ec65.jpg?alt=media&token=5b4f9ad7-1e99-42b8-966d-50c74fc2eab6: open failed: ENOENT (No such file or directory)
如果我打开此链接,我在那里看到图像,问题是为什么它没有在图像视图中设置
and if I open this link I see image there, question is why it is not set in image view
推荐答案
setImageURI()
用于Android特有的内容URI.
平台,而不是用于指定Internet资源的URI.
setImageURI()
is for content URIs particular to the Android
platform, not URIs specifying Internet resources.
尝试在新线程中从Internet获取位图,然后将其添加到ImageView.像这样:
Try getting your bitmap from internet in a new thread an then add it to your ImageView. Like this:
uploadedImage.setImageBitmap(getImageBitmap(mDownloadUrl));
private Bitmap getImageBitmap(String url) {
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e(TAG, "Error getting bitmap", e);
}
return bm;
}
您还可以使用一个有用的库来设置名为Picasso的图像(内部和外部图像) http://square .github.io/picasso/
You also can use a useful library to set image (Internal and external images) called Picasso http://square.github.io/picasso/
这篇关于设置图像uri导致无法解码流:java.io.FileNotFoundException:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!