为什么我的位图没有显示在ImageView中?

时间:2014-05-03 03:51:53

标签: android image

我正在尝试为用户显示缩略图,但它无法正常工作。如果用户选择“照片”,则他或她在MainActivity上拍照,然后将URI传递给NewActivity。以下是我传递数据的方式:

这是MainActivity

onOptionsItemSelected(MenuItem item)

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 startActivityForResult(takePictureIntent, ACTION_TAKE_PHOTO_S);

这也在MainActivity

onActivityResult(int requestCode, int resultCode,
        Intent intent)

 case ACTION_TAKE_PHOTO_S:
    if (resultCode == Activity.RESULT_OK) {
        Intent i = new Intent(getBaseContext(), NewActivity.class);
        Uri imageUri = intent.getData();
        i.putExtra("imageUri", imageUri.toString());
        startActivity(i);
    }
    break;

我在NewActivity

中收到此URI
onCreate(Bundle savedInstanceState)

 Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String imageuri = extras.getString("imageUri");

        try {
            ImageView mImageView = (ImageView) findViewById(R.id.image_view);
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(imageuri));
            mImageView.setImageBitmap(bitmap);
            mImageView.setVisibility(View.VISIBLE);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

这是我的activity_create.xml布局的一部分(ImageView所在的位置)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="45"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="100dp"
        android:layout_height="75dp"
        android:layout_gravity="center"
        android:scaleType="centerCrop"
        android:visibility="gone" />

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="20dp"
        android:visibility="gone" />

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="20dp"
        android:visibility="gone" />

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:visibility="gone" />

    <ImageButton
        android:id="@+id/delete_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:layout_marginRight="20dp"
        android:src="@drawable/ic_action_remove"
        android:visibility="visible" />
</LinearLayout>
</LinearLayout>

我搜索了所有Stack Overflow并找不到答案。我尝试记录NewActivity onCreate(Bundle savedInstanceState)中的“位图”和“imageuri”变量,并获得以下内容:

表示“位图”

android.graphics.Bitmap@42feb668

表示“imageuri”

content://media/external/images/media/596

所以,我知道图像在活动之间正确传递,但即使我将其可见性设置为可见,它也不会在ImageView中显示。

修改

我尝试手动设置我的ImageView(这是NewActivity第68行)

mImageView.setImageURI(Uri.parse("content://media/external/images/media/596"));

这给了我以下错误

05-03 00:19:46.368: E/AndroidRuntime(32064): FATAL EXCEPTION: main
05-03 00:19:46.368: E/AndroidRuntime(32064): Process: com.lschlessinger.myspots, PID: 32064
05-03 00:19:46.368: E/AndroidRuntime(32064): java.lang.OutOfMemoryError
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:709)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:685)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.graphics.drawable.Drawable.createFromStream(Drawable.java:840)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.widget.ImageView.resolveUri(ImageView.java:675)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.widget.ImageView.setImageURI(ImageView.java:409)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at com.lschlessinger.myspots.NewActivity.onCreate(NewActivity.java:68)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.app.Activity.performCreate(Activity.java:5451)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.app.ActivityThread.access$900(ActivityThread.java:169)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.os.Looper.loop(Looper.java:136)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at android.app.ActivityThread.main(ActivityThread.java:5476)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at java.lang.reflect.Method.invokeNative(Native Method)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at java.lang.reflect.Method.invoke(Method.java:515)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
05-03 00:19:46.368: E/AndroidRuntime(32064):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

根据outofmemory错误,显然应用程序无法从特定URI加载位图,因为它的大小。如果要在应用程序中加载它,则需要调整图像大小!有多种方法可以解决这个问题,您可以使用android:largeHeap=”true”来解决清单文件中的应用程序标记。但是您不应该这样做,您应该通过将图像大小调整为所需的大小来解决它。此外,您可以按照这些帖子中提到的这些解决方案:

MEMORY LEAK AND OUT OF MEMORY ERROR

MEMORY LEAK AND OUT OF MEMORY ERROR USING LRU CACHE

MEMORY LEAK AND OUT OF MEMORY ERROR USING DISK LRU CACHE

MEMORY LEAK AND OUT OF MEMORY ERROR USING LIST,LINKEDLIST AND HASHMAP

答案 1 :(得分:2)

如果您不想自己处理位图的大小调整,还有各种库可以为您执行此操作。例子是: https://github.com/novoda/ImageLoader#getting-the-library

在那里,您还可以动态地将图像大小调整为手机的特定尺寸。