2016-04-20 36 views
0

問題:用戶可以拍攝/選擇最多3張照片。我在計算如何填寫3個案例方面遇到困難;我不知道如何才能檢索相應的ImageView ID。Android - 拍攝/選擇圖片並將其放入正確的圖像視圖

Here is the UI

我試過putextra因爲我使用 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE),但它似乎是不可能使用putextra方法(我不獲取任何額外)

所以讓我分享與您的代碼,並隨時讓我知道,如果你會不同地繼續下去。非常感謝!

所以我在這裏捕捉點擊事件並將V.getID傳遞給將處理與選擇/拍照相關的操作的方法。

@Override 
public void onClick(View v) { 

    switch(v.getId()){ 
     case R.id.add_item_give_button: 
      checkAddedItem(); 
      break; 
     case R.id.add_item_image_1: 
      selectImage(v.getId()); 
      break; 
     case R.id.add_item_image_2: 
      selectImage(v.getId()); 
      break; 
     case R.id.add_item_image_3: 
      selectImage(v.getId()); 
      break; 
    } 
} 

的selectImage方法被調用,將處理alertDialog如果用戶想將要求要麼拍照或選擇一個。我試圖通過ID在putExtra方法,但沒有在startActivityForResult

public void selectImage(final int imageViewID){ 

    final CharSequence[] options = {getString(R.string.cameral_select_photo_label), getString(R.string.camera_take_photo_label), getString(R.string.common_cancel_label)}; 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    builder.setTitle(getString(R.string.camera_dialog_title_label)); 

    builder.setItems(options, new DialogInterface.OnClickListener() { 
     @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1) 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      if(options[which].equals(getString(R.string.camera_take_photo_label))){ 

       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       intent.putExtra("ImageViewID", imageViewID); 
       startActivityForResult(intent, REQUEST_CAMERA); 

      } 
      else if(options[which].equals(getString(R.string.cameral_select_photo_label))){ 

       Utils.verifyStoragePermissions(getActivity()); 

       Intent intent = new Intent(
         Intent.ACTION_PICK, 
         MediaStore.Images.Media.EXTERNAL_CONTENT_URI 
       ); 

       intent.setType("image/*"); 
       startActivityForResult(
         Intent.createChooser(intent, getResources().getText(R.string.camera_select_image)),SELECT_FILE); 

      } 
      else if(options[which].equals(getString(R.string.common_cancel_label))){ 

       dialog.dismiss(); 

      } 
     } 
    }); 
    builder.show(); 
} 

在startActivityForResult收到,我沒有收到ImageViewID。所以現在,我只是將圖像放在第一個ImageView中,因爲我無法檢索正確的ID。

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

    if(resultCode == Activity.RESULT_OK){ 

     if(requestCode == REQUEST_CAMERA){ 

      Log.d("Data content", String.valueOf(data)); 

      Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
      ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
      thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes); 

      File destination = new File(Environment.getExternalStorageDirectory(), 
        System.currentTimeMillis() + ".jpg"); 

      FileOutputStream fo; 

      try { 
       destination.createNewFile(); 
       fo = new FileOutputStream(destination); 
       fo.write(bytes.toByteArray()); 
       fo.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      itemPic1.setImageBitmap(thumbnail); 

     } else if (requestCode == SELECT_FILE){ 

      Log.d("imageViewOrigin", String.valueOf(data.getIntExtra("imageViewID", 0))); 

      Uri selectedImageUrl = data.getData(); 
      String[] projection = {MediaStore.MediaColumns.DATA}; 
      CursorLoader cursorLoader = new CursorLoader(getContext(), selectedImageUrl, projection, null, null, null); 
      Cursor cursor = cursorLoader.loadInBackground(); 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); 
      cursor.moveToFirst(); 

      String selectedImagePath = cursor.getString(column_index); 

      Bitmap bm; 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(selectedImagePath, options); 
      final int REQUIRED_SIZE = 200; 
      int scale = 1; 
      while(options.outWidth/scale/2 >= REQUIRED_SIZE && options.outHeight/scale/2 >= REQUIRED_SIZE) 
       scale += 2; 
      options.inSampleSize = scale; 
      options.inJustDecodeBounds = false; 
      bm = BitmapFactory.decodeFile(selectedImagePath, options); 

      itemPic1.setImageBitmap(bm); 


     } 

    } 

} 

回答

0

我會推薦在ImageViews上設置標籤。按照鏈接,它的類似問題What is the main purpose of setTag() getTag() methods of View?。讓我知道你是否需要更多的幫助!

+0

謝謝Feddy,但我怕我會面臨同樣的問題。不管怎麼說,還是要謝謝你! – Isabelle

+0

對不起,我想我現在明白了。取而代之的是設置單擊監聽器,就像創建單獨的TextView對象作爲公共類對象一樣。例如TextView firstTextView =(TextView)findViewById(R.id。「id here」);從那裏你可以添加單個點擊監聽器,一旦他們initalized。只需簡單地把firstTextView.setOnClick,然後Android Studio將自動填充其餘的給你。 (閱讀下面的評論) – fsebek

+0

然後,你可以調用該方法,並通過該方法的文本視圖。 public void selectImage(final int imageViewID,TextView clickedTextView)。然後在該方法中,您可以簡單地將位圖設置爲傳遞的文本視圖。讓我知道你是否需要更多的幫助!這麼晚纔回復很抱歉。由於它是漫長的,所以它做了2條評論。 – fsebek

0

試試這個方法:

private void openImageIntent(int IMAGE_TYPE) { 

    // Determine Uri of camera image to save. 
    final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "mycapturedImage" + File.separator); 
    root.mkdirs(); 
    final String fname = getUniqueImageFilename(); 
    final File sdImageMainDirectory = new File(root, fname); 
    outputFileUri = Uri.fromFile(sdImageMainDirectory); 

    // Camera. 
    final List<Intent> cameraIntents = new ArrayList<Intent>(); 
    final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    final PackageManager packageManager = getPackageManager(); 
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0); 

    for (ResolveInfo res : listCam) { 
     final String packageName = res.activityInfo.packageName; 
     final Intent intent = new Intent(captureIntent); 
     intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); 
     intent.setPackage(packageName); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
     cameraIntents.add(intent); 
    } 

    // Filesystem. 
    final Intent galleryIntent = new Intent(); 
    galleryIntent.setType("image/*"); 
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT); 

    // Chooser of filesystem options. 
    final Intent chooserIntent = Intent.createChooser(galleryIntent, "Choisir une Source"); 

    // Add the camera options. 
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()])); 

    startActivityForResult(chooserIntent, IMAGE_TYPE); 
} 

然後檢索每個圖片是這樣的:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode != RESULT_CANCELED) { 
     if (resultCode == RESULT_OK) { 
      if (requestCode == FIRST_IMAGE_INTENT) { 
       final boolean isCamera; 
       if (data == null) { 
        isCamera = true; 

       } else { 
        final String action = data.getAction(); 
        if (action == null) { 
         isCamera = false; 
        } else { 
         isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
        } 
       } 


       if (isCamera) { 

        selectedCaptureUri = outputFileUri; 

       } else { 

        selectedCaptureUri = data == null ? null : data.getData(); 


       } 

    //Display image here 

      } else if (requestCode == SECOND_PICTURE_INTENT) {...} 
+0

謝謝bashizip,我現在就試試這個,並且會讓你知道它是否有效:) – Isabelle

相關問題