【问题标题】:user Intent send image path to another Activity用户意图将图像路径发送到另一个活动
【发布时间】:2013-11-27 11:16:52
【问题描述】:

有时我在手机上运行应用程序。问题是当我在相机拾取照片后单击“确定”按钮时,此时!应用程序停止运行!事实上,我想在另一个 Activity 上查看图片!是

拍照:

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

    }

}

预览图像活动!getIntent() 是否设置为空?

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

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

【问题讨论】:

  • 检查你的额外键值变量SAVED_IMAGE_DIR_PATHKEY_PHOTO_PATH是否相同,
  • 关闭应用时出现什么错误...??
  • 请同时发布错误。很可能它将是一个空指针。请发布日志并提及错误出现在哪一行。由于此处没有可用的行,请告诉错误即将发生的代码
  • SAVED_IMAGE_DIR_PATH 和 KEY_PHOTO_PATH 都是不同的参数 beteew 活动!
  • 那段代码完全没问题吗?

标签: android android-intent


【解决方案1】:

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 = "";
}  

4 - 现在使用

在您的活动中显示该图像
byte[] mybits=Const.imgbyte;
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeByteArray(mybits, 0, mybits.length, options);
yourImageview.setImageBitmap(bitmap);

【讨论】:

  • 我的 App 的 logcat :java.lang.RuntimeException: Unable to resume activity {com.example.newpingziyi/com.example.newpingziyi.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131073, result=-1, data=null} to activity {com.example.newpingziyi/com.example.newpingziyi.MainActivity}: java.lang.NullPointerException
  • 你试过上面的代码了吗??使用 Const.java 文件将图像字节传输到其他活动?
  • 我不是,那些朋友想看Logcat,所以...,现在我开始了
  • Const.dbSrNo!**dbSrNo** 那是什么
  • Bitmap bm = BitmapFactory.decodeFile("这里你可以写你的图片路径")....这是我的图片路径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多