Android 如何从图库中选择图像并在其他活动中显示?

Android 如何从图库中选择图像并在其他活动中显示?,android,android-camera,android-imageview,android-gallery,Android,Android Camera,Android Imageview,Android Gallery,嗨,我正在使用相机和画廊按钮。当我选择照相机时,我会打开照相机,拍照并通过intent将照片发送到下一个活动。这个很好用 但当我选择gallery按钮时,它会打开gallery图像,但不会在下一个活动中显示图像。没有错误。只是一个空白的黑屏 它显示为, 我/编舞:跳过2103帧!应用程序可能在其主线程上做了太多工作 请帮忙解决这个问题 谢谢 public class TestCameraActivity extends AppCompatActivity { private Ca

嗨,我正在使用相机和画廊按钮。当我选择照相机时,我会打开照相机,拍照并通过intent将照片发送到下一个活动。这个很好用

但当我选择gallery按钮时,它会打开gallery图像,但不会在下一个活动中显示图像。没有错误。只是一个空白的黑屏

它显示为, 我/编舞:跳过2103帧!应用程序可能在其主线程上做了太多工作

请帮忙解决这个问题

谢谢

    public class TestCameraActivity extends AppCompatActivity {
    private CameraPreview mImageSurfaceView;
    public Camera camera = null;
    public static final int MEDIA_TYPE_IMAGE = 1;
    private static final int REQUEST_CAMERA_PERMISSION = 1;
    private static final int REQUEST_WRITE_PERMISSION = 2;
    private FrameLayout cameraPreviewLayout;
    private float mDist;
    private Bitmap bm;
    private float ratio = 9f / 16f;
    private Button gallery;
    private int  SELECT_FILE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_camera);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        cameraPreviewLayout = (FrameLayout) findViewById(R.id.camera_preview);
        Button captureButton = (Button) findViewById(R.id.button);
        gallery = (Button) findViewById(R.id.galleries);
        gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);//
                startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);
            }
        });
        captureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                camera.takePicture(null, null, pictureCallback);
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        requestCameraPermission();
    }
    Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            bm = BitmapFactory.decodeByteArray(data, 0, data.length);
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                // Setting post rotate to 90
                Matrix mtx = new Matrix();
                mtx.postRotate(90);
                bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), mtx, true);
                bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), (int) (bm.getWidth() * ratio));
            } else {// LANDSCAPE MODE
                //No need to reverse width and height
                Bitmap scaled = Bitmap.createScaledBitmap(bm, bm.getWidth(), bm.getHeight(), true);
                bm = scaled;
            }
            if (bm == null) {
                Toast.makeText(TestCameraActivity.this, "khoong duoc roi", Toast.LENGTH_LONG).show();
                return;
            }
//           capturedImageHolder.setImageBitmap(bm);

            Intent intent = new Intent(TestCameraActivity.this, CaptureResultActivity.class);
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG, 60, bs);
            intent.putExtra("bitmap", bs.toByteArray());
            startActivity(intent);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestWritePermission();
            } else {
                asyncSave();
            }
//            camera.startPreview();
        }
    };

    public void asyncSave() {
        new AsyncTask<Bitmap, Void, Boolean>() {
            @Override
            protected Boolean doInBackground(Bitmap... bitmaps) {
                return saveBitmapImage(bitmaps[0]);
            }

            @Override
            protected void onPostExecute(Boolean aBoolean) {
                if (aBoolean) {
                    Toast.makeText(TestCameraActivity.this, "Save Image Success", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(TestCameraActivity.this, "Failed to save image...", Toast.LENGTH_SHORT).show();
                }
            }
        }.execute(bm);
    }

    private boolean saveBitmapImage(Bitmap bitmap) {
        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null) {
            Log.d("BBB", "Error creating media file, check storage permissions: ");
            return false;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
//                fos.write(data);
            fos.close();
            galleryAddPic(pictureFile);
            return true;
        } catch (FileNotFoundException e) {
            Log.d("BBB", "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d("BBB", "Error accessing file: " + e.getMessage());
        }
        return false;
    }

    private static File getOutputMediaFile(int type) {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "MyCameraApp");
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_LINH" + timeStamp + ".jpg");
        } else {
            return null;
        }

        return mediaFile;
    }

    // zoom
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Get the pointer ID
        Camera.Parameters params = camera.getParameters();
        int action = event.getAction();


        if (event.getPointerCount() > 1) {
            // handle multi-touch events
            if (action == MotionEvent.ACTION_POINTER_DOWN) {
                mDist = getFingerSpacing(event);
            } else if (action == MotionEvent.ACTION_MOVE && params.isZoomSupported()) {
                camera.cancelAutoFocus();
                handleZoom(event, params);
            }
        } else {
            // handle single touch events
            if (action == MotionEvent.ACTION_UP) {
                handleFocus(event, params);
            }
        }
        return true;
    }

    private void handleZoom(MotionEvent event, Camera.Parameters params) {
        int maxZoom = params.getMaxZoom();
        int zoom = params.getZoom();
        float newDist = getFingerSpacing(event);
        if (newDist > mDist) {
            //zoom in
            if (zoom < maxZoom)
                zoom++;
        } else if (newDist < mDist) {
            //zoom out
            if (zoom > 0)
                zoom--;
        }
        mDist = newDist;
        params.setZoom(zoom);
        camera.setParameters(params);
    }

    public void handleFocus(MotionEvent event, Camera.Parameters params) {
        int pointerId = event.getPointerId(0);
        int pointerIndex = event.findPointerIndex(pointerId);
        // Get the pointer's current position
        float x = event.getX(pointerIndex);
        float y = event.getY(pointerIndex);

        List<String> supportedFocusModes = params.getSupportedFocusModes();
        if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
            camera.autoFocus(new Camera.AutoFocusCallback() {
                @Override
                public void onAutoFocus(boolean b, Camera camera) {
                    // currently set to auto-focus on single touch
                }
            });
        }
    }

    /**
     * Determine the space between the first two fingers
     */
    private float getFingerSpacing(MotionEvent event) {
        // ...
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    }

    //Request Permission

    private void requestCameraPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.CAMERA)) {
                } else {
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.CAMERA},
                            REQUEST_CAMERA_PERMISSION);
                }
            } else {
                camera = Camera.open();
                mImageSurfaceView = new CameraPreview(this, camera);
                cameraPreviewLayout.addView(mImageSurfaceView);
            }
        } else {
            camera = Camera.open();
            mImageSurfaceView = new CameraPreview(this, camera);
            cameraPreviewLayout.addView(mImageSurfaceView);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CAMERA_PERMISSION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    camera = Camera.open(0);
                    mImageSurfaceView = new CameraPreview(this, camera);
                    cameraPreviewLayout.addView(mImageSurfaceView);

                } else {
                    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }
            case REQUEST_WRITE_PERMISSION: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    asyncSave();
                } else {
                    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

    //Request Write Permission

    private void requestWritePermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Here, thisActivity is the current activity
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                // Should we show an explanation?
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        REQUEST_WRITE_PERMISSION);
//                }
            } else {
                asyncSave();
            }
        } else {
            //api < 21
        }
    }

    //Add photo to the gallery
    private void galleryAddPic(File f) {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {

            if (resultCode == Activity.RESULT_OK) {
                if (requestCode == SELECT_FILE)
                    onSelectFromGalleryResult(data);
            }




        } catch (Exception e) {
            Toast.makeText(this, "Please try again", Toast.LENGTH_LONG)
                    .show();
        }
    }
    private static Bitmap StringToBitMap(String encodedString){
        try {
            byte [] encodeByte= Base64.decode(encodedString,Base64.DEFAULT);
            Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
            return bitmap;
        } catch(Exception e) {
            e.getMessage();
            return null;
        }
    }
    private void onSelectFromGalleryResult(Intent data) {

        Bitmap bm=null;
        if (data != null) {
            try {
                bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
            } catch (IOException e) {
                e.printStackTrace();
            }
            Intent intent = new Intent(TestCameraActivity.this,    CaptureResultActivity.class);
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG, 60, bs);
            intent.putExtra("bitmap", bs.toByteArray());
            startActivity(intent);
        }
    }
    }  
公共类TestCameraActivity扩展了AppCompatActivity{
私人摄像审查;
公共摄像机=空;
公共静态最终int媒体类型图像=1;
私有静态最终int请求\摄像机\权限=1;
私有静态最终int请求\写入\权限=2;
专用框架布局摄像机回顾布局;
私人浮动mDist;
私有位图bm;
私人浮动比率=9f/16f;
私人按钮库;
私有int选择_文件=1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test_摄像头);
setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u Picture);
cameraPreviewLayout=(框架布局)findViewById(R.id.camera\U预览);
按钮捕获按钮=(按钮)findViewById(R.id.Button);
gallery=(按钮)findViewById(R.id.galleries);
gallery.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意图=新意图();
intent.setType(“image/*”);
intent.setAction(intent.ACTION\u GET\u CONTENT)//
startActivityForResult(Intent.createChooser(Intent,“选择文件”),选择文件);
}
});
captureButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
照相/摄像(空,空,pictureCallback);
}
});
}
@凌驾
受保护的void onResume(){
super.onResume();
requestCameraPermission();
}
Camera.PictureCallback PictureCallback=新建Camera.PictureCallback(){
@凌驾
公共void onPictureTaken(字节[]数据,摄像头){
bm=位图工厂.decodeByteArray(数据,0,数据.length);
if(getResources().getConfiguration().orientation==Configuration.orientation\u纵向){
//设置柱旋转至90度
矩阵mtx=新矩阵();
mtx.旋转后(90);
bm=Bitmap.createBitmap(bm,0,0,bm.getWidth(),bm.getHeight(),mtx,true);
bm=Bitmap.createBitmap(bm,0,0,bm.getWidth(),(int)(bm.getWidth()*比率));
}else{//横向模式
//不需要反转宽度和高度
位图缩放=位图.createScaledBitmap(bm,bm.getWidth(),bm.getHeight(),true);
bm=标度;
}
如果(bm==null){
Toast.makeText(TestCameraActivity.this,“koong duoc roi”,Toast.LENGTH_LONG.show();
返回;
}
//capturedImageHolder.setImageBitmap(bm);
意向意向=新意向(TestCameraActivity.this、CaptureResultActivity.class);
ByteArrayOutputStream bs=新建ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG,60,bs);
intent.putExtra(“位图”,bs.toByteArray());
星触觉(意向);
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
requestWritePermission();
}否则{
asyncSave();
}
//camera.startPreview();
}
};
公共void asyncSave(){
新建异步任务(){
@凌驾
受保护的布尔doInBackground(位图…位图){
返回saveBitmapImage(位图[0]);
}
@凌驾
受保护的void onPostExecute(布尔aBoolean){
if(阿布奥兰语){
Toast.makeText(TestCameraActivity.this,“保存图像成功”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(TestCameraActivity.this,“未能保存图像…”,Toast.LENGTH_SHORT.show();
}
}
}.执行(bm);
}
私有布尔存储位图(位图){
File pictureFile=getOutputMediaFile(媒体类型图像);
如果(pictureFile==null){
Log.d(“BBB”,“创建媒体文件时出错,请检查存储权限:”);
返回false;
}
试一试{
FileOutputStream fos=新的FileOutputStream(pictureFile);
compress(bitmap.CompressFormat.PNG,100,fos);
//写入(数据);
fos.close();
galleryAddPic(图片文件);
返回true;
}catch(filenotfounde异常){
Log.d(“BBB”,“未找到文件:”+e.getMessage());
}捕获(IOE异常){
Log.d(“BBB”,“访问文件时出错:”+e.getMessage());
}
返回false;
}
私有静态文件getOutputMediaFile(int类型){
File mediaStorageDir=新文件(Environment.getExternalStoragePublicDirectory(
环境。目录(图片),“MyCameraApp”);
如果(!mediaStorageDir.exists()){
如果(!mediaStorageDir.mkdirs()){
Log.d(“MyCameraApp”,“创建目录失败”);
返回null;
}
}
//创建媒体文件名
字符串时间戳=新的SimpleDateFormat(“yyyyMMdd_HHmmss”)。格式(新日期();
文件媒体文件;
如果(类型==媒体类型图像){
媒体文件=ne
if (Build.VERSION.SDK_INT < 19) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
} else {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("image/*");
    startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_IMAGE_REQUEST
            && resultCode == Activity.RESULT_OK) {
        Uri uri = data.getData();
        mImageCaptureUri = Uri.fromFile(new File(uriToFilename(uri)));
    }
}

private String uriToFilename(Uri uri) {
    String path = null;

    if (Build.VERSION.SDK_INT < 11) {
        path = RealPathUtil.getRealPathFromURI_BelowAPI11(getActivity(), uri);
    } else if (Build.VERSION.SDK_INT < 19) {
        path = RealPathUtil.getRealPathFromURI_API11to18(getActivity(), uri);
    } else {
        path = RealPathUtil.getRealPathFromURI_API19(getActivity(), uri);
    }
    return path;
}
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;

public class RealPathUtil {
@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri) {
    String filePath = "";
    if (DocumentsContract.isDocumentUri(context, uri)) {
        String wholeID = DocumentsContract.getDocumentId(uri);
        // Split at colon, use second item in the array
        String[] splits = wholeID.split(":");
        if (splits.length == 2) {
            String id = splits[1];

            String[] column = { MediaStore.Images.Media.DATA };
            // where id is equal to
            String sel = MediaStore.Images.Media._ID + "=?";
            Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    column, sel, new String[] { id }, null);
            int columnIndex = cursor.getColumnIndex(column[0]);
            if (cursor.moveToFirst()) {
                filePath = cursor.getString(columnIndex);
            }
            cursor.close();
        }
    } else {
        filePath = uri.getPath();
    }
    return filePath;
}

@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    String result = null;
    CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();
    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        result = cursor.getString(column_index);
    }
    return result;
}

public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
}