2014-05-03 77 views
0

我想顯示用戶的縮略圖,但它不起作用。如果用戶選擇「照片」,則他或她在MainActivity上拍攝照片,然後將該URI傳遞給NewActivity。這裏是我想過去的數據:爲什麼我的位圖不在ImageView中顯示?

這是在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

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(); 
     } 

    } 
收到此URI

這裏是[部分]我的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> 

我已經找遍了所有堆棧溢出,找不到答案。我試圖記錄從NewActivityonCreate(Bundle savedInstanceState)的 「位圖」 和 「imageuri」 變量,得到了以下幾點:

爲 「位圖」

[email protected] 

爲 「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) 
+0

什麼是你的位圖的大小/尺寸? – Geros

+0

取這個字符串,用Uri.parse(someStr)''把'Uri'對象放進一個'並使用'mImageView.setImageURI(imgUri);' – razpeitia

+0

@ Glenn--它是5312 x 2988(寬x高) – lschlessinger

回答

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

相關問題