当前位置: 首页 > 知识库问答 >
问题:

用户意图将图像路径发送到另一个活动

谷梁涵忍
2023-03-14

拍照:

private void takePhoto() {
    String SDState = Environment.getExternalStorageState();
    if (SDState.equals(Environment.MEDIA_MOUNTED)) {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        ContentValues values = new ContentValues();
        photoUri = getActivity().getContentResolver().insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
        startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            Toast.makeText(getActivity(), R.string.take_photo_rem,
                    Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(getActivity(), R.string.takePhoto_msg,
                Toast.LENGTH_LONG).show();
    }
}

相册:

private void pickPhoto() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
}

OnActivityResult:用户意图发送图像uri

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        doPhoto(requestCode, data);
    }
    super.onActivityResult(requestCode, resultCode, data);
}

private void doPhoto(int requestCode, Intent data) {

    if (requestCode == SELECT_PIC_BY_PICK_PHOTO) {
        if (data == null) {
            Toast.makeText(getActivity(), R.string.photo_err,
                    Toast.LENGTH_LONG).show();
            return;
        }
        photoUri = data.getData();
        if (photoUri == null) {
            Toast.makeText(getActivity(), R.string.photo_err,
                    Toast.LENGTH_LONG).show();
            return;
        }
    }
    String[] pojo = { MediaStore.Images.Media.DATA };
    Cursor cursor = getActivity().managedQuery(photoUri, pojo, null, null,
            null);
    if (cursor != null) {
        int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
        cursor.moveToFirst();
        picPath = cursor.getString(columnIndex);
        try {
            if (Integer.parseInt(Build.VERSION.SDK) < 14) {

                cursor.close();
            }
        } catch (Exception e) {
            Log.e(TAG, "error:" + e);
        }
    }
    Log.i(TAG, "imagePath = " + picPath);
    if (picPath != null) {

        Intent startEx = new Intent(getActivity(), PhotoPre.class);
        Bundle bundle = new Bundle();
        bundle.putString(SAVED_IMAGE_DIR_PATH, picPath);
        startEx.putExtras(bundle);
        startActivity(startEx);

    } else {
        Toast.makeText(getActivity(), R.string.photo_err, Toast.LENGTH_LONG)
                .show();

    }

}
Bundle bundle = getIntent().getExtras();
    picPath = bundle.getString(KEY_PHOTO_PATH);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    bm = BitmapFactory.decodeFile(picPath, options);

共有1个答案

充子航
2023-03-14

1-启动相机拍摄图像

Intent cameraIntent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment .getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, Const.dbSrNo + "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);   
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 
startActivityForResult(cameraIntent, CAMERA_REQUEST);

2-在“OnActivityResult”中编写下面的代码

if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
    File imgFile = new File(Environment.getExternalStorageDirectory(),
            "/MyImages/");

    /*photo = BitmapFactory.decodeFile(imgFile.getAbsolutePath() + "/"
            + Const.dbSrNo + "image.jpg");*/

    BitmapFactory.Options options = new BitmapFactory.Options();

    options.inSampleSize = 4;
    options.inPurgeable=true;
    Bitmap bm = BitmapFactory.decodeFile(imgFile.getAbsolutePath() + "/"
            + Const.dbSrNo + "image.jpg",options);

    imageView2.setImageBitmap(bm);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object 

    byteImage_photo = baos.toByteArray(); 

    Const.imgbyte=byteImage_photo;  

3-生成一个java文件const.java

public class Const {
    public static byte[] imgbyte = "";
}  
byte[] mybits=Const.imgbyte;
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeByteArray(mybits, 0, mybits.length, options);
yourImageview.setImageBitmap(bitmap);
 类似资料:
  • 我试图将图像从一个活动发送到另一个活动,但我不知道如何设置ImageView。

  • 问题内容: 我对android非常陌生,我正在尝试将用户输入的数据(他们的名字)发送到另一个活动。过去,我可以使用Intent在活动之间发送单行代码,但是我无法解决如何向两个不同的TextView发送两个不同的字符串。 这是到目前为止我的MainActivity代码: 我第二项活动MainGame的代码: 当我运行它时,我得到了两个TextView中都为“ name2”添加的内容。我需要做些什么来

  • 问题内容: 我正在尝试将图像从一项活动发送到另一项活动,但我不知道如何设置imageview。 这是我发送图像和其他内容的方式 这是我尝试读取图像并将其设置为ImageView的方法,但是出现语法错误。 我应该如何设置ImageView? 问题答案: 你不觉得 应该

  • 问题内容: 如何使用Intent类的方法将自定义类型的对象从一个传递到另一个? 问题答案: 如果您只是传递对象,那么Parcelable就是为此而设计的。与使用Java的本机序列化相比,使用它需要付出更多的努力,但是它的速度更快(我的意思是,WAY更快)。 在文档中,有关如何实现的一个简单示例是: 请注意,如果要从给定的宗地中检索多个字段,则必须按照将其放入的相同顺序(即,采用FIFO方法)进行操

  • Activity-2(将所选图像设置为屏幕背景图像的活动)

  • 我想把意图转移到Xamarin.Android中的另一个活动。基本上,我需要Intent.data和Intent.clipdata到另一个活动,所以我使用下面的代码来传输Intent,但我不知道检索它的正确方法。 下面是Main Activity中的代码 在第二活动中 如何在第二个活动中检索意图?