2016-04-20 32 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

相关问题