2011-11-15 22 views
0

我能夠從SD卡中的GridView打開我的圖像,也能夠通過使用下列代碼映像路徑傳遞到另一個活動:要打開在主要活動中的被點擊圖片到另一個活動

package com.example.sdcardimage; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class Imageshow extends Activity { 


    private Cursor cursor; 
    private int columnIndex; 
    TextView text; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main1); 
     text = (TextView)findViewById(R.id.text); 
     // Set up an array of the Thumbnail Image ID column we want 
     String[] projection = {MediaStore.Images.Thumbnails._ID}; 
     // Create the cursor pointing to the SDCard 
     cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
       projection, // Which columns to return 
       null,  // Return all rows 
       null, 
       MediaStore.Images.Thumbnails.IMAGE_ID); 
     // Get the column index of the Thumbnails Image ID 
     columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); 


     GridView sdcardImages = (GridView) findViewById(R.id.sdcard); 
     sdcardImages.setAdapter(new ImageAdapter(this)); 

     // Set up a click listener 
     sdcardImages.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView parent, View v, int position, long id) { 
       // Get the data location of the image 
       String[] projection = {MediaStore.Images.Media.DATA}; 
       cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
         projection, // Which columns to return 
         null,  // Return all rows 
         null, 
         null); 
       columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
       cursor.moveToPosition(position); 
       // Get image filename 
       String imagePath = cursor.getString(columnIndex); 
       text.setText(imagePath); 
       Intent i = new Intent(getBaseContext(), preview.class); 
       i.putExtra("Value1", imagePath); 
         startActivity(i); 

       } 
     }); 
    } 

    /** 
    * Adapter for our image files. 
    */ 
    private class ImageAdapter extends BaseAdapter { 

     private Context context; 

     public ImageAdapter(Context localContext) { 
      context = localContext; 
     } 

     public int getCount() { 
      return cursor.getCount(); 
     } 
     public Object getItem(int position) { 
      return position; 
     } 
     public long getItemId(int position) { 
      return position; 
     } 
     public View getView(final int position, View convertView, ViewGroup parent) 
     { 
      ImageView picturesView; 
      if (convertView == null) 
      { 
       picturesView = new ImageView(context); 
       // Move cursor to current position 
       cursor.moveToPosition(position); 
       // Get the current value for the requested column 
       int imageID = cursor.getInt(columnIndex); 
       // Set the content of the image based on the provided URI 
       picturesView.setImageURI(Uri.withAppendedPath(
         MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID)); 
       picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
       picturesView.setPadding(8, 8, 8, 8); 
       picturesView.setLayoutParams(new GridView.LayoutParams(100, 100)); 
      } 
      else 
      { 
       picturesView = (ImageView)convertView; 
      } 
      return picturesView; 
     } 
    } 
} 

現在,當我使用從上面的代碼中獲得的路徑中的ImageView打開圖像這讓我空指針異常我在使用中獲得的路徑的另一個活動的開場圖片代碼如下:

package com.example.sdcardimage; 

import java.io.BufferedInputStream; 
import java.io.FileInputStream; 


import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class preview extends Activity 
{ 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main2); 
     ImageView picture = (ImageView)findViewById(R.id.imagepath); 
     Bundle extras = getIntent().getExtras(); 
     if (extras == null) { 
      return; 
     } 
     String value1 = extras.getString("Value1"); 
     if (value1 != null) 
     { 
     TextView text = (TextView)findViewById(R.id.text); 
     text.setText(value1); 
     FileInputStream in; 
     BufferedInputStream buf;  
    try{ 
     in = new FileInputStream(value1); 
     buf = new BufferedInputStream(in); 
     Bitmap bMap = BitmapFactory.decodeStream(buf); 
     Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, 150, 100, true); 
     picture.setImageBitmap(bMapScaled); 
     if (in != null) 
     { 
      in.close(); 
      } 
      if (buf != null) { 
      buf.close(); 
      } 
     } catch (Exception e) { 
      Log.e("Error reading file", e.toString()); 
     } 
     } 
    } 
} 

所以任何一個可以請提供我解決了爲什麼我得到空指針異常和n OT能在imageView.Thanks在advance.Also顯示圖像,請幫助我快速的迫切需要我..

我的日誌如下

11-15 13:59:14.764: ERROR/AndroidRuntime(823): Uncaught handler: thread main exiting due to uncaught exception 
11-15 13:59:14.814: ERROR/AndroidRuntime(823): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sdcardimage/com.example.sdcardimage.preview}: java.lang.NullPointerException 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268) 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284) 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  at android.app.ActivityThread.access$1800(ActivityThread.java:112) 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692) 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  at android.os.Looper.loop(Looper.java:123) 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  at android.app.ActivityThread.main(ActivityThread.java:3948) 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  at java.lang.reflect.Method.invoke(Method.java:521) 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  at dalvik.system.NativeStart.main(Native Method) 
11-15 13:59:14.814: ERROR/AndroidRuntime(823): Caused by: java.lang.NullPointerException 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  at com.example.sdcardimage.preview.onCreate(preview.java:32) 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231) 
11-15 13:59:14.814: ERROR/AndroidRuntime(823):  ... 11 more 

Yuipee我得到了輸出什麼,我一直在尋找..所以我張貼我的修改後的第二活性如下的代碼,因此,這將有助於其他人誰正在尋找同樣的事情:

package com.example.sdcardimage; 

import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileInputStream; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.widget.ImageView; 


public class preview extends Activity 
{ 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main2); 
     Intent intent = getIntent(); 
     Bundle extras = intent.getExtras(); 
     String filename = extras.getString("Value1"); 
     FileInputStream is = null; 
     BufferedInputStream bis = null; 
     try { 
      is = new FileInputStream(new File(filename)); 
      bis = new BufferedInputStream(is); 
      Bitmap bitmap = BitmapFactory.decodeStream(bis); 
      ImageView image = (ImageView)findViewById(R.id.preview); 
      image.setImageBitmap(bitmap);  
      } 
     catch (Exception e) { 
      //Try to recover 
     } 
     finally { 
      try { 
       if (bis != null) { 
        bis.close(); 
       } 
       if (is != null) { 
        is.close(); 
       }  
      } catch (Exception e) { 
      } 
     } 
    } 
} 
+0

請與異常的詳細信息添加logcat的日誌。 – MByD

+0

你檢查了你在下一個活動中獲得的圖像路徑嗎?並且相同的路徑能夠向您顯示之前活動中的圖像? – Hiral

+0

我已經檢查了第一個活動中的路徑,它與我在第二個活動中獲得的路徑相同,當我使用textview在代碼中顯示圖像路徑時... –

回答

0

使用此代碼

Intent intent = getIntent(); 
Bundle extras = intent.getExtras(); 
String filename = extras.getString("filename"); 
Bitmap originalBitmap = BitmapFactory.decodeFile(filename); 
ImageView myimage = (ImageView) findViewById(R.id.ImageView01); 
myimage.setImageBitmap(originalBitmap); 

最終解決

Intent intent = getIntent(); 
    Bundle extras = intent.getExtras(); 
    String filename = extras.getString("Value1"); 
    FileInputStream is = null; 
    BufferedInputStream bis = null; 
    try { 
     is = new FileInputStream(new File(filename)); 
     bis = new BufferedInputStream(is); 
     Bitmap bitmap = BitmapFactory.decodeStream(bis); 
     ImageView image = (ImageView)findViewById(R.id.preview); 
     image.setImageBitmap(bitmap);  
     } 
    catch (Exception e) { 
     //Try to recover 
    } 
    finally { 
     try { 
      if (bis != null) { 
       bis.close(); 
      } 
      if (is != null) { 
       is.close(); 
      }  
     } catch (Exception e) { 
     } 
    } 
+0

對不起,但仍然會得到同樣的錯誤。 .. –

+0

在Imageshow活動中使用不同的遊標(在onItemClickListener之前,在..之前,我在你的代碼中進行了更改,檢查一次.. –

+0

我沒有得到你在我的代碼中所做的更改,請你指出在我的代碼之前在這些變化之前寫評論... –