我尝试通过从图库或相机中选择将图像加载到ImageView中,但遗憾的是尺寸太大,所以我压缩它并尝试加载它。但是图像仍然没有加载。我试过毕加索但是我没有知道如何使用Picasso将位图加载到ImageView中。请帮我解决。
代码:
fetch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
builder.setTitle("Select from");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);//zero can be replaced with any action code
break;
case 1:
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);//one can be replaced with any action code
break;
}
alertDialog.dismiss();
}
});
alertDialog=builder.create();
alertDialog.show();
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 0:
if (resultCode==RESULT_OK)
{
upld_img.setImageBitmap(compressImage(data.getData()));
Log.e("Image","Uploaded");
}
break;
case 1:
if (resultCode==RESULT_OK)
{
upld_img.setImageBitmap(compressImage(data.getData()));
// upld_img.setImageURI(data.getData());
Log.e("Image","Uploaded");
}
break;
}
}
public Bitmap compressImage(Uri uri){
InputStream inputStream=null;
try{
inputStream=getContext().getContentResolver().openInputStream(uri);
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
Bitmap bmp= BitmapFactory.decodeStream(inputStream);
bmp.compress(Bitmap.CompressFormat.PNG,100,byteArrayOutputStream);
byteArrayOutputStream.close();
inputStream.close();
}
catch (FileNotFoundException e){
} catch (IOException e) {
e.printStackTrace();
}
return bmp;
}
ctgry_details_lyt.xml(ID = upload_img):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="@dimen/hmpge_img_dimen"
android:layout_height="@dimen/hmpge_img_dimen"
android:id="@+id/upload_img"
android:layout_centerHorizontal="true"
android:layout_margin="@dimen/hmpge_img_mrgn"
android:layout_gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fetch"
android:layout_below="@+id/upload_img"
android:text="@string/fetch"
android:textSize="@dimen/txt_sze"
android:layout_margin="@dimen/hmpge_img_mrgn"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/upload"
android:layout_below="@+id/fetch"
android:text="@string/upload"
android:textSize="@dimen/txt_sze"
android:layout_margin="@dimen/hmpge_img_mrgn"/>
<EditText
android:layout_width="match_parent"
android:layout_height="@dimen/edt_txt_hght"
android:background="@drawable/edtxt_bg"
android:textColor="#ffffff"
android:id="@+id/img_id"
android:layout_below="@+id/upload"
android:hint="@string/image_id"
android:paddingLeft="5dp"
android:layout_margin="@dimen/hmpge_img_mrgn"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/download"
android:layout_below="@+id/img_id"
android:text="@string/download"
android:textSize="@dimen/txt_sze"
android:layout_margin="@dimen/hmpge_img_mrgn"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/download"
android:layout_centerHorizontal="true"
android:layout_gravity="center">
<ImageView
android:layout_width="@dimen/hmpge_img_dimen"
android:layout_height="@dimen/hmpge_img_dimen"
android:background="@drawable/user"
android:id="@+id/dwnld_img"
android:layout_centerHorizontal="true"
android:layout_margin="@dimen/hmpge_img_mrgn"
android:layout_gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/save"
android:text="@string/save"
android:textSize="@dimen/txt_sze"
android:layout_below="@+id/dwnld_img"
android:layout_margin="@dimen/hmpge_img_mrgn"/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
答案 0 :(得分:0)
您可以使用uri和picasso直接加载图片。
Picasso.with(getActivity()).load(data.getData()).into(upld_img);
答案 1 :(得分:0)
试试这个,
在
中的您的清单文件中添加以下行<application
....
android:largeHeap="true"
...
</application>
因为它可以访问大尺寸的文件, 也许对你有用