2014-07-02 33 views
0

我有一個應用程序,您可以從圖庫或設備上的照片文件夾中選擇圖像。所選文件的路徑存儲在Intent中,以便它們可以在各個活動之間傳遞。我通過intent.getDataString()訪問路徑。BitmapFactory decodeFile FileNotFoundException

一旦我有所有選定的路徑圖像,我將它們存儲在ArrayList中,並將其傳遞給ImageAdapter,以顯示在ListView中。

我geting FileNotFoundException,有沒有人有任何想法爲什麼?

在此先感謝

Matt。

import java.util.ArrayList; 

import uk.co.mobilewebexpert.infowrapsynclibrary.ApplicationObj; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ListView; 

public class QueuedImagesActivity extends Activity { 

    private static final String TAG = QueuedImagesActivity.class.getSimpleName(); 
    private ImageAdapter adapter; 
    private ListView imageList; 
    ApplicationObj appObj; 
    Intent[] photos; 
    String path; 

    private ArrayList<String> imagePaths= new ArrayList<String>(); // Edit your code here.. 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.image_listview); 

     appObj = (ApplicationObj) getApplication(); 

     boolean includeBeingProcessed = true; 

     try { 
      photos = appObj.getQueuedPhotos(includeBeingProcessed); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     for(int i = 0; i < photos.length; i++){ 

      path = photos[i].getDataString(); 
      imagePaths.add(path); 

      Log.e(TAG, "path in QueuedImagesActivity = " + path); 

     } 




     imageList= (ListView) findViewById(R.id.listView1); 
     adapter= new ImageAdapter(getBaseContext(), imagePaths); 
     imageList.setAdapter(adapter);  
    } 
} 

import java.util.ArrayList; 

import android.content.Context; 
import android.graphics.BitmapFactory; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 

public class ImageAdapter extends BaseAdapter { 

    private static final String TAG = ImageAdapter.class.getSimpleName(); 

    static class RowItemHolder{ 
     ImageView imageView; 
    } 
    private Context context; 
    private ArrayList<String> imagePaths= new ArrayList<String>(); 

    public ImageAdapter(Context baseContext, ArrayList<String> imagePaths) { 
    // TODO Auto-generated constructor stub 
     this.context= baseContext; 
     this.imagePaths= imagePaths; 
    } 

    @Override 
    public int getCount() { 
    // TODO Auto-generated method stub 
     return imagePaths.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
    // TODO Auto-generated method stub 
      return null; 
    } 

    @Override 
    public long getItemId(int position) { 
    // TODO Auto-generated method stub 
      return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    View view; 
    view= convertView; 
    RowItemHolder holder = null; 
    if(view== null){ 
      LayoutInflater in =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = in.inflate(R.layout.image_view, parent, false); 
      holder= new RowItemHolder(); 
      holder.imageView=(ImageView) view.findViewById(R.id.imageView1); 
     view.setTag(holder); 
    } else{ 
      holder = (RowItemHolder) view.getTag(); 
    } 
    //Edit the code here according to you needs.. 
    //like creating option and converting to Bitmap, 
    //or you can do this job in the main activity. 
    //holder.imageView.setImageResource(imagePaths.get(position)); 

    Log.e(TAG, "imagePaths.get(position) = " + imagePaths.get(position)); 


    holder.imageView.setImageBitmap(BitmapFactory.decodeFile(imagePaths.get(position))); 

    return view; 
} 
} 

07-02 07:51:33.941: E/QueuedImagesActivity(22700): path in QueuedImagesActivity = content://media/external/images/media/7496 
07-02 07:51:33.951: E/ImageAdapter(22700): imagePaths.get(position) = content://media/external/images/media/7496 
07-02 07:51:33.961: E/BitmapFactory(22700): Unable to decode stream: FileNotFoundException 
07-02 07:51:33.971: E/ImageAdapter(22700): imagePaths.get(position) = content://media/external/images/media/7496 
07-02 07:51:33.971: E/BitmapFactory(22700): Unable to decode stream: FileNotFoundException 
07-02 07:51:33.981: E/ImageAdapter(22700): imagePaths.get(position) = content://media/external/images/media/7496 
07-02 07:51:33.981: E/BitmapFactory(22700): Unable to decode stream: FileNotFoundException 
07-02 07:51:33.991: E/ImageAdapter(22700): imagePaths.get(position) = content://media/external/images/media/7496 
07-02 07:51:33.991: E/BitmapFactory(22700): Unable to decode stream: FileNotFoundException 

回答

3

您正在獲取的路徑不是真正的路徑圖片是你要設置它,它的ImageView設置它像

imageView.setImageURI(Uri.parse(imagePaths.get(position))); 

一個Uri.If獲得通過傳遞你的URI的真實路徑,將其設置爲ImageView

private String getRealPathFromURI(Uri contentURI) { 
String result; 
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null); 
if (cursor == null) { // Source is Dropbox or other similar local file path 
    result = contentURI.getPath(); 
} else { 
    cursor.moveToFirst(); 
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    result = cursor.getString(idx); 
    cursor.close(); 
} 
return result; 
} 

對於這裏Uri to path conversion

+0

嘿謝謝,它現在工作正常。 – turtleboy

0

你的問題是appObj.getQueuedPhotos-它沒有返回文件名,其返回的URI,decodeFile預計的路徑。在這種情況下,您可以快速執行字符串操作並從前面刪除內容:/但您最好將URI修復到getQueuedPhotos函數中

0

content://media/external/images/media/7496多個檢查是不是存儲的文件的實際位置,但只是開放的,因此機器人找不到指定的路徑上的文件。但是,您可以使用URI獲取文件的絕對路徑,並在解碼時使用該路徑。

使用此方法,將獲得的絕對路徑:

public String getRealPathFromURI(Context context, Uri contentUri) { 
    Cursor cursor = null; 
    try { 
    String[] proj = { MediaStore.Images.Media.DATA }; 
    cursor = context.getContentResolver().query(contentUri, proj, null, null, null); 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
    } finally { 
    if (cursor != null) { 
     cursor.close(); 
    } 
    } 
} 

來源:https://stackoverflow.com/a/3414749/1239966

1

它會導致因爲intent.getDataString()返回的URI的字符串。改爲使用intent.getData().getPath()

0

試試這種方式,希望這會幫助你解決你的問題。

public String getAbsolutePath(Uri uri) { 
     if(Build.VERSION.SDK_INT >= 19){ 
      String id = uri.getLastPathSegment().split(":")[1]; 
      final String[] imageColumns = {MediaStore.Images.Media.DATA }; 
      final String imageOrderBy = null; 
      Uri tempUri = getUri(); 
      Cursor imageCursor = getContentResolver().query(tempUri, imageColumns, 
        MediaStore.Images.Media._ID + "="+id, null, imageOrderBy); 
      if (imageCursor.moveToFirst()) { 
       return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA)); 
      }else{ 
       return null; 
      } 
     }else{ 
      String[] projection = { MediaColumns.DATA }; 
      Cursor cursor = getContentResolver().query(uri, projection, null, null, null); 
      if (cursor != null) { 
       int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); 
       cursor.moveToFirst(); 
       return cursor.getString(column_index); 
      } else 
       return null; 
     } 

    } 
0

檢查輸入流,你傳遞的只是一個URI。

BitmapFactory.decodeFile(imagePaths.get(position)); 

因此找不到文件,獲取圖像的絕對路徑並傳遞它。