捕获/选择图像并返回其类型 [英] Capturing/Selecting images and returning its type
问题描述
我怎样才能让用户在拍摄图像或从图库中选择之间进行选择,我想知道照片的类型之后(PNG / JPG)。
我使用这个code,但它无法正常工作。
@覆盖
公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,结果code,数据);
如果(结果code == getActivity()。RESULT_OK){
如果(要求code == REQUEST_CAMERA){
。位图的照片=(位图)data.getExtras()获得(数据);
mImg.setImageBitmap(照片);
}否则如果(要求code == SELECT_FILE){
乌里selectedImageUri = data.getData();
的String [] =投影{MediaStore.MediaColumns.DATA};
CursorLoader cursorLoader =新CursorLoader(getActivity(),selectedImageUri,投影,NULL,NULL,
空值);
光标光标= cursorLoader.loadInBackground();
INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst(); 字符串selectedImagePath = cursor.getString(Column_Index中); 位图BM;
BitmapFactory.Options选项=新BitmapFactory.Options();
options.inJustDe codeBounds = TRUE;
BitmapFactory.de codeFILE(selectedImagePath,期权);
最终诠释REQUIRED_SIZE = 200;
int标= 1;
而(options.outWidth /规模/ 2 - = REQUIRED_SIZE
&功放;&安培; options.outHeight /规模/ 2 - = REQUIRED_SIZE)
规模* = 2;
options.inSampleSize =规模;
options.inJustDe codeBounds = FALSE;
BM = BitmapFactory.de codeFILE(selectedImagePath,期权); mImg.setImageBitmap(BM);
mImg.setAlpha(1); }
}
}
我想后来知道照片的类型(PNG / JPG)
块引用>呼叫
的getType()
在ContentResolver的
,传递乌里
。
我使用这个code,但它无法正常工作。
块引用>删除你最有什么,因为你的
selectedImagePath
code将无法大多数Android设备上,并且您的主应用程序线程的位图解码。使用图像加载库喜欢的毕加索处理图像加载你的,异步的,包括缩放。毕加索可以使用乌里
直接,没有任何的瑕疵selectedImagePath
的东西。然后,所有你需要的是的getType()
调用来获取MIME类型的图像。整个请求code == SELECT_FILE
块将2-3线code来代替。How can I allow the user to choose between capturing an image or selecting it from the gallery, and I want to know the type of the photo afterwards (PNG/JPG). I'm using this code but it is not working well.
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == getActivity().RESULT_OK) { if (requestCode == REQUEST_CAMERA) { Bitmap photo = (Bitmap) data.getExtras().get("data"); mImg.setImageBitmap(photo); } else if (requestCode == SELECT_FILE) { Uri selectedImageUri = data.getData(); String[] projection = {MediaStore.MediaColumns.DATA}; CursorLoader cursorLoader = new CursorLoader(getActivity(), selectedImageUri, projection, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); cursor.moveToFirst(); String selectedImagePath = cursor.getString(column_index); Bitmap bm; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(selectedImagePath, options); final int REQUIRED_SIZE = 200; int scale = 1; while (options.outWidth / scale / 2 >= REQUIRED_SIZE && options.outHeight / scale / 2 >= REQUIRED_SIZE) scale *= 2; options.inSampleSize = scale; options.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(selectedImagePath, options); mImg.setImageBitmap(bm); mImg.setAlpha(1); } } }
解决方案I want to know the type of the photo afterwards (PNG/JPG)
Call
getType()
on aContentResolver
, passing in theUri
.I'm using this code but it is not working well.
Delete most of what you have, as your
selectedImagePath
code will fail on most Android devices and you are decoding the bitmap on the main application thread. Use an image-loading library like Picasso to handle the image loading for you, asynchronous, including the scaling. Picasso can use theUri
directly without any of the flawedselectedImagePath
stuff. Then, all you need is thegetType()
call to get the MIME type of the image. Your entirerequestCode == SELECT_FILE
block will be replaced by 2-3 lines of code.这篇关于捕获/选择图像并返回其类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!