2013-06-19 124 views
2

我是android開發的noob,我試圖在ImageButton中顯示來自SDCard的圖像。我的清單中有READ_EXTERNAL_STORAGE權限,我正在使用aFileChooser庫來查找我的SD卡上的文件。當我點擊我的圖像按鈕時,它允許我選擇我想要的圖像,但不顯示圖像按鈕中的圖像。 ImageButton完全消失。我沒有收到錯誤,但ImageButton變爲空白。任何幫助是極大的讚賞。爲什麼不從SDCard顯示圖像?

private void showChooser() { 
     // Use the GET_CONTENT intent from the utility class 
     Intent target = FileUtils.createGetContentIntent(); 
     // Create the chooser Intent 
     Intent intent = Intent.createChooser(
       target, getString(R.string.chooser_title)); 
     try { 
      startActivityForResult(intent, REQUEST_CODE); 
     } catch (ActivityNotFoundException e) { 
      // The reason for the existence of aFileChooser 
     }    
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     switch (requestCode) { 
     case REQUEST_CODE: 
      // If the file selection was successful 
      if (resultCode == RESULT_OK) {  
       if (data != null) { 
        // Get the URI of the selected file 
        final Uri uri = data.getData(); 

        try { 
         // Create a file instance from the URI 
         final File file = FileUtils.getFile(uri); 
         Toast.makeText(RegisterActivity.this,"File Selected: "+file.getAbsolutePath(), Toast.LENGTH_LONG).show(); 
         Log.e("File Path", file.getAbsolutePath());// Returns /external/images/media/1830 
         Log.e("BMP NULL", bmp.toString()); //Throws NullPointerException 
         Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());//Seems to work fine here 
         userpic.setImageBitmap(bmp); //ImageButton disappears/goes blank here. 
        } catch (Exception e) { 
         Log.e("FileSelectorTestActivity", "File select error", e); 
        } 
       } 
      } 
      break; 
     } 
     super.onActivityResult(requestCode, resultCode, data); 
    } 
+0

代碼userpic初始化,並使用findviewbyid分配給一個看法? – ChrisBorg

+0

是的。我在onCreate期間這樣做。 – DollaBill

回答

0

我可以幫你解決沒有文件選擇... 希望這將有助於

只檢查數據!= NULL, 和發送請求的代碼!

public final int SELECT_PICTURE = 324234; //some value 

第一 ...

 ContentValues values = new ContentValues(); 
     String title = "blabla"; 
     String descritpion = "blabla"; 
     values.put(MediaStore.Images.Media.TITLE, title); 
     values.put(MediaStore.Images.Media.DESCRIPTION, descritpion); 
     imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
     Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
     startActivityForResult(takePhotoIntent, SELECT_PICTURE); 

...

然後

if (requestCode == SELECT_PICTURE && data != null) 
    try { 
        // !!! could be every format!!! 
        Uri selectedImage = data.getData(); 
        String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
        if (cursor == null) { 
         Toast.makeText(getApplicationContext(),"imageunguilty", Toast.LENGTH_SHORT).show(); 
         break; 
        } 
        cursor.moveToFirst(); 
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
        String picturePath = cursor.getString(columnIndex); 
        cursor.close(); 
        File src = new File(picturePath); 
        Bitmap b = decodeFile(src.getAbsolutePath()); 
        //here recommended to resize your bitmap due to OutOfMemoryError!!! 
        if (b != null) { 
         // UpdatePicture 

        } else { 
         Toast.makeText(getApplicationContext(), "no image selected?", Toast.LENGTH_SHORT).show(); 
        } 
       } catch (OutOfMemoryError e) { 
        Log.e("Pictures", "" + e.getMessage()); 
       } 
在你的代碼 Log.e( 「BMP NULL」

,bmp.toString ()); //拋出NullPointerException異常 因爲bmp之前沒有定義!

1

對於aFileChooser Android插件,當您將其分配給您的程序時,您需要添加活動。 你還沒有添加以下清單中

<activity 
      android:name="com.ipaulpro.afilechooser.FileChooserActivity" 
      android:exported="false" 
      android:icon="@drawable/ic_chooser" 
      android:label="@string/choose_file" 
      android:theme="@style/ChooserTheme" > 
      <intent-filter> 
       <action android:name="android.intent.action.GET_CONTENT" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
       <category android:name="android.intent.category.OPENABLE" /> 

       <data android:mimeType="*/*" /> 
      </intent-filter> 
</activity>