2012-09-10 26 views
0

我有這個代碼的問題,在實踐中我試圖從手機庫中只讀取文件名image2.jpg image3.jpg ...等。 但是這個代碼給我錯誤如何從手機庫中讀取圖像文件

這是代碼:

public static Uri getRandomImage(ContentResolver resolver) { 

    String secondo = "image2.jpg"; 
    String terzo = "image3.jpg"; 
    String quarto = "image4.jpg"; 
    String quinto = "image5.jpg"; 
    String sesto = "image6.jpg"; 
    String settimo = "image7.jpg"; 
    String ottavo = "image8.jpg"; 

    String[] projection = new String[] { 
     BaseColumns._ID, 
     MediaColumns.DATA, 

    }; 



    Uri uri = Media.EXTERNAL_CONTENT_URI; 

    Cursor cursor = Media.query(resolver, uri, projection, null, MediaColumns._ID); 
    if (cursor == null || cursor.getCount() <= 0) { 
      return null; 
    } 

    cursor.moveToPosition(new Random().nextInt(cursor.getCount())); 

    String completefilepath = cursor.getString(cursor.getColumnIndex(MediaColumns.DATA)); 
    if(completefilepath == secondo || completefilepath == terzo || completefilepath == quarto || 
      completefilepath == quinto || completefilepath == sesto || completefilepath == settimo || 
      completefilepath == ottavo){ 

     string = cursor.getString(1); 

    } 
    return Uri.withAppendedPath(uri, string); 
} 

這是logcat的:

E/AndroidRuntime(9147): FATAL EXCEPTION: main 
E/AndroidRuntime(9147): java.lang.IllegalStateException: Unknown URL: content://media/external/images/media/null 
E/AndroidRuntime(9147):  at android.os.Parcel.readException(Parcel.java:1268) 
E/AndroidRuntime(9147):  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160) 
E/AndroidRuntime(9147):  at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:125) 
E/AndroidRuntime(9147):  at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:538) 
E/AndroidRuntime(9147):  at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:467) 
E/AndroidRuntime(9147):  at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:380) 
E/AndroidRuntime(9147):  at it.bisemanuDEV.slidepuzzle.TileView.getImageFromUri(TileView.java:559) 
E/AndroidRuntime(9147):  at it.bisemanuDEV.slidepuzzle.TileView.newGame(TileView.java:156) 
E/AndroidRuntime(9147):  at it.bisemanuDEV.slidepuzzle.SlidePuzzleActivity.onOptionsItemSelected(SlidePuzzleActivity.java:377) 
E/AndroidRuntime(9147):  at android.app.Activity.onMenuItemSelected(Activity.java:2762) 
E/AndroidRuntime(9147):  at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730) 
E/AndroidRuntime(9147):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143) 
E/AndroidRuntime(9147):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855) 
E/AndroidRuntime(9147):  at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532) 
E/AndroidRuntime(9147):  at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122) 
E/AndroidRuntime(9147):  at android.view.View$PerformClick.run(View.java:8819) 
E/AndroidRuntime(9147):  at android.os.Handler.handleCallback(Handler.java:603) 
E/AndroidRuntime(9147):  at android.os.Handler.dispatchMessage(Handler.java:92) 
E/AndroidRuntime(9147):  at android.os.Looper.loop(Looper.java:123) 
E/AndroidRuntime(9147):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
E/AndroidRuntime(9147):  at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime(9147):  at java.lang.reflect.Method.invoke(Method.java:521) 
E/AndroidRuntime(9147):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
E/AndroidRuntime(9147):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
E/AndroidRuntime(9147):  at dalvik.system.NativeStart.main(Native Method) 
+0

請上傳錯誤日誌爲好。 – Lucifer

+0

編輯我的帖子與代碼和logcat – David

回答

0
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
       photoPickerIntent.setType("image/*"); 
       startActivityForResult(photoPickerIntent, int requestCode); 
+0

對不起,但我不明白如何使用這個你的意見,可以更清楚? – David

0

你可以參考這個,並根據你的需要進行更改!

XML ::

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ImageView 
     android:id="@+id/imgView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"></ImageView> 
    <Button 
     android:id="@+id/buttonLoadPicture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:text="Load Picture" 
     android:layout_gravity="center"></Button> 
</LinearLayout> 

類文件::

import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 

public class ImageGalleryDemoActivity extends Activity { 


    private static int RESULT_LOAD_IMAGE = 1; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture); 
     buttonLoadImage.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       Intent i = new Intent(
         Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

       startActivityForResult(i, RESULT_LOAD_IMAGE); 
      } 
     }); 
    } 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String picturePath = cursor.getString(columnIndex); 
      cursor.close(); 

      ImageView imageView = (ImageView) findViewById(R.id.imgView); 
      imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

     } 


    } 
} 
+0

我不會完全扭曲我的代碼。所有我需要能夠修復方法代碼getRandomImage從我創建的文件夾中讀取文件,其中我複製了這些圖像文件。這個文件夾的路徑是:/ sdcard/DCIM/Wallpaper / – David