2016-10-01 50 views
0

我想設置與從相機和畫廊收到的結果數據imageview。相機意圖不工作在我的代碼

我有4個ImageViews,所以根據選定的圖像視圖請求代碼的變化..它被分配給這個捕獲和畫廊變量。

private void picselect(int capture, int gallery) { 
    final CharSequence[] items = {"Take Photo", "Choose from Library", 
      "Cancel"}; 

    a=capture; 
    b=gallery; 
    AlertDialog.Builder builder = new AlertDialog.Builder(
      EditProfile.this); 
    builder.setItems(items, new DialogInterface.OnClickListener() { 


     @Override 
     public void onClick(DialogInterface dialog, int item) { 
      if (items[item].equals("Take Photo")) { 
       Calendar cal = Calendar.getInstance(); 
       File file = new File(Environment.getExternalStorageDirectory(), 
         (cal.getTimeInMillis() + ".jpg")); 
       if (!file.exists()) { 
        try { 
         file.createNewFile(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } else { 

        file.delete(); 
        try { 
         file.createNewFile(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
       uricam = Uri.fromFile(file); 
       Intent takePicture = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       Log.d("shanOutput", String.valueOf(uricam)); 
       takePicture.putExtra(MediaStore.EXTRA_OUTPUT, uricam); 
       startActivityForResult(takePicture,a); 


      } else if (items[item].equals("Choose from Library")) { 

       Intent photoPic = new Intent(Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       startActivityForResult(photoPic,b); 


      } else if (items[item].equals("Cancel")) { 
       dialog.dismiss(); 
      } 
     } 
    }); 
    builder.show(); 

} 

private String getRealPathFromURI(Uri contentURI) { 
    String result; 
    Cursor cursor = getContentResolver().query(contentURI, null, 
      null, null, null); 

    if (cursor == null) { 

     result = contentURI.getPath(); 
    } else { 
     cursor.moveToFirst(); 
     int idx = cursor 
       .getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
     result = cursor.getString(idx); 
     cursor.close(); 
    } 
    return result; 
} 

這裏面onActivityResult

if (resultCode == RESULT_OK) { 

       if(imageReturnedIntent!=null){ 
        Uri selectedImage = imageReturnedIntent.getData(); 
        if(selectedImage!=null){ 

         profileImage.setImageURI(selectedImage);} 
        sendImageToServer(String.valueOf(getRealPathFromURI(selectedImage)),1); 

        Log.d("shanImage", String.valueOf(getRealPathFromURI(selectedImage))); 
       } 


      } 

書面接收圖片從畫廊工作正常,但部分被返回null我的代碼。我在代碼中缺少什麼。有人可以指導我完成這個模塊。

+0

a =捕捉; b = gallery;需要int值這一個 – Saveen

回答

0

ACTION_IMAGE_CAPTURE不會返回Uri通過Intent交付給onActivityResult()

由於您使用的是EXTRA_OUTPUT,因此您應該知道圖像的位置:位於您放入EXTRA_OUTPUT的位置。所以,看看你的形象。

This sample application演示使用EXTRA_OUTPUT。特別是,它使用FileProvider,因爲一旦targetSdkVersion上升到24或更高,就不能在Android 7.0及更高版本上使用Uri.fromFile()

+0

你能給我建議我應該在那裏更換什麼 –