簡體   English   中英

用戶意圖將圖像路徑發送到另一個活動

[英]user Intent send image path to another Activity

有時我在手機上運行App。問題是此時相機拍照后單擊“確定”按鈕,此時App停止運行!實際上,我想在另一個Activity上看到Pic!

拍照片:

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()設置為null?

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-啟動相機拍攝圖像

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM