我正在尝试将图片从图库加载到imageview,但它无法正常工作。当我从手机图库中选择任何图像时。它给出了
位图太大而无法上传到纹理
MainActivity
public class MainActivity extends AppCompatActivity {
Button button;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
// imageView = (ImageView)findViewById(R.id.imageview);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galeri_int = new Intent();
galeri_int.setType("image/*");
galeri_int.setAction(Intent.ACTION_GET_CONTENT);
galeri_int.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(galeri_int,44);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
// super.onActivityResult(requestCode,resultCode,data);
ImageView imageview = (ImageView)findViewById(R.id.imageview);
Bitmap bitmap = null;
if(bitmap != null){
bitmap.recycle();
}
InputStream stream = null;
try {
stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
imageview.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="gc.uploaderimager.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:src="@drawable/abc"
android:id="@+id/imageview"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:id="@+id/button"
android:layout_below="@+id/imageview"
/>
</RelativeLayout>
我该如何解决这个问题?