2013-12-20 61 views
1

我在調用照片庫意圖之後使用以下方法獲取圖像。我想通過傳遞該圖像的位圖並在第二個活動中顯示該圖像來開始另一個活動。但是,從照片庫中挑選照片後,什麼都不會發生。無法從主要活動中的onActivityResult方法開始新活動

protected void onActivityResult(int requestCode, int resultCode, Intent 
        data) { 
     final String path; 

     if (requestCode == GALLERY_REQUEST) { 
     if (resultCode == Activity.RESULT_OK) { 

     Uri imageFileUri = data.getData(); 

      if (imageFileUri != null) { 
       try { 
       path = getPath(imageFileUri); 
       BitmapFactory.Options load_option = new BitmapFactory.Options(); 
       load_option.inPurgeable = true; 
       load_option.inDensity = 0; 
       load_option.inTargetDensity = 0; 
       load_option.inDensity = 0; 
       load_option.inScaled = false; 
       bmp_main = BitmapFactory.decodeFile(path, load_option); 
       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       bmp_main.compress(Bitmap.CompressFormat.PNG, 100, stream); 
       byte[] byteArray = stream.toByteArray(); 

       Intent intentPhoto = new Intent(MainActivity.this, SecondActivity.class); 
       intentPhoto.putExtra("image",byteArray); 
       startActivity(intentPhoto); 

        } catch (Exception e) { 

        e.printStackTrace(); 
        } 
       } 
      } 

      } 


     } 

有人可以告訴有什麼問題嗎?

注:1.I已經添加了活動清單

2.沒有對此

3.我已經做了調試logcat的錯誤或異常,它會高達startActivity行是否正確,但沒有什麼發生後

+1

發表您的logcat –

+0

你確定你要進入你如果條件?嘗試記錄imageFileUri值。 –

+1

代碼看起來不錯,在那裏放一些日誌語句可能會幫助你進一步。 –

回答

2

使用下面的代碼...

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK 
      && null != data) { 
     Uri contentUri = data.getData(); 
     startActivity(new Intent(this, ViewGallery_Photo.class) 
       .setData(contentUri)); 
    } 

所以在onActivityResult有2個邏輯

1)如果要加載從畫廊圖像,然後它會帶你到其他活動

2)如果你是捕獲從相機圖像,然後它會帶你與其他活動不這是由庫調用相同的活動......

+0

您可以使用我的代碼做些什麼,我想要的是有一個photoButton,點擊哪個PhotoGallery會打開。當我選擇任何照片時,它會在另一個活動中查看。希望你現在明確我的問題... – CrazyLearner

+0

我已經更新了我的答案,你只需在你的onActivityReslut中添加這個條件,而不是ViewGallery_Photo.class寫你的Activity.class ..你完成了你想要的... – InnocentKiller

+0

那麼請告訴我如何從第二個活動檢索contentUri並將其轉換爲位圖 – CrazyLearner

1

調用此出你的如果條件:

Intent intentPhoto = new Intent(MainActivity.this, SecondActivity.class); 
intentPhoto.putExtra("image",byteArray); 
startActivity(intentPhoto); 

試試這個:

  try { 
      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(); 

      String fullPath = Environment.getExternalStorageDirectory() 
        .getAbsolutePath(); 
      try { 

       File dir = new File(fullPath); 
       if (!dir.exists()) { 
        dir.mkdirs(); 
       } 
       OutputStream fOut = null; 
       File file = new File(fullPath, "userImage" + ".png"); 
       if (file.exists()) 
        file.delete(); 
       file.createNewFile(); 
       fOut = new FileOutputStream(file); 
       fOut.flush(); 
       fOut.close(); 
       Log.v("Image saved", "in" + file); 
      } catch (Exception e) { 
       Log.e("saveToExternalStorage()", e.getMessage()); 
      } 

      decodeFile(picturePath); 
      /* 
      * iv_display.setImageBitmap(mPhoto); Bitmap useThisBitmap = 
      * Bitmap.createScaledBitmap(mPhoto, mPhoto.getWidth(), 
      * mPhoto.getHeight(), true); 
      */ 

      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      myBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
      bytepicture = baos.toByteArray(); 

      Intent newdata = new Intent(MainMenu.this, SecondActivity.class); 
      newdata.putExtra("picture", bytepicture); 
      startActivity(newdata); 
     } catch (Exception e) { 
      // TODO: handle exception 
      Log.v("TAG", "No Image Selected:"); 
     } 

對於解碼文件:

public void decodeFile(String filePath) { 

    // Decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filePath, o); 

    // The new size we want to scale to 
    final int REQUIRED_SIZE = 1024; 

    // Find the correct scale value. It should be the power of 2. 
    int width_tmp = o.outWidth, height_tmp = o.outHeight; 
    int scale = 3; 
    while (true) { 
     if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) 
      break; 
     width_tmp /= 2; 
     height_tmp /= 2; 
     scale *= 2; 
    } 

    // Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    mPhoto = BitmapFactory.decodeFile(filePath, o2); 
    myBitmap = ExifUtils.rotateBitmap(filePath, mPhoto); 

    // image.setImageBitmap(bitmap); 
} 
+0

if條件之外,我怎樣才能得到byteArray? – CrazyLearner

+0

使其成爲全球..... – Piyush

+0

什麼都沒有發生...... – CrazyLearner

相關問題