无法解码 android 中的流 java.io.FileNotFoundException?
Unable to decode stream java.io.FileNotFoundException in android?
我是 android 的新手,当我尝试使用 decodefile()
.
时遇到此错误
这是代码
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode == PICK_FROM_GALLERY ) && (resultCode == RESULT_OK) && (data != null)){
selectedImageUri = data.getData( );
Log.e("Path1", ""+selectedImageUri);
path1 = selectedImageUri.getPath();
file = new File(path1);
path =file.getAbsolutePath();
Log.e("Path2", file.getAbsolutePath());
mImageView.setImageURI(selectedImageUri);
}}
protected void badButtonPressed() {
final long startTime = SystemClock.uptimeMillis();
BitmapFactory.Options options = new BitmapFactory.Options();
// Part 1: Decode image
>> Bitmap unscaledBitmap = BitmapFactory.decodeFile(path, options);
imageHeight = options.outHeight;
imageWidth = options.outWidth;
Log.e("w", ""+imageWidth);
Log.e("h", ""+imageHeight);
请告诉我我的错误。
请尝试一下。
int PICK_FROM_GALLERY = 3;
String path;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FROM_GALLERY
&& resultCode == Activity.RESULT_OK) {
try {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
path = cursor.getString(columnIndex);
cursor.close();
} catch (NullPointerException e) {
}
}
}
public void pickFromGallery()
{
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_FROM_GALLERY);
}
找到这个答案
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, SELECT_IMAGE);
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_IMAGE) {
try {
selectedImagePath = getAbsolutePath(intent.getData());
mImageCaptureUri = intent.getData();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public String getAbsolutePath(Uri uri) {
String[] projection = {MediaStore.MediaColumns.DATA};
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
我是 android 的新手,当我尝试使用 decodefile()
.
这是代码
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode == PICK_FROM_GALLERY ) && (resultCode == RESULT_OK) && (data != null)){
selectedImageUri = data.getData( );
Log.e("Path1", ""+selectedImageUri);
path1 = selectedImageUri.getPath();
file = new File(path1);
path =file.getAbsolutePath();
Log.e("Path2", file.getAbsolutePath());
mImageView.setImageURI(selectedImageUri);
}}
protected void badButtonPressed() {
final long startTime = SystemClock.uptimeMillis();
BitmapFactory.Options options = new BitmapFactory.Options();
// Part 1: Decode image
>> Bitmap unscaledBitmap = BitmapFactory.decodeFile(path, options);
imageHeight = options.outHeight;
imageWidth = options.outWidth;
Log.e("w", ""+imageWidth);
Log.e("h", ""+imageHeight);
请告诉我我的错误。
请尝试一下。
int PICK_FROM_GALLERY = 3;
String path;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FROM_GALLERY
&& resultCode == Activity.RESULT_OK) {
try {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
path = cursor.getString(columnIndex);
cursor.close();
} catch (NullPointerException e) {
}
}
}
public void pickFromGallery()
{
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_FROM_GALLERY);
}
找到这个答案
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, SELECT_IMAGE);
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_IMAGE) {
try {
selectedImagePath = getAbsolutePath(intent.getData());
mImageCaptureUri = intent.getData();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public String getAbsolutePath(Uri uri) {
String[] projection = {MediaStore.MediaColumns.DATA};
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}