Android 在活动之间传输图像的最快方式

Android 在活动之间传输图像的最快方式,android,android-intent,android-activity,android-bitmap,Android,Android Intent,Android Activity,Android Bitmap,我是android编程的新手。我使用两种方式在活动之间传输图像,即使用intent或创建文件,但传输图像并在第二个活动的imageview中显示大约需要3或4秒。有没有什么方法可以比这更快的传输速度,因为很多应用程序(如whatsapp)的传输速度更快。我的代码如下。任何帮助都将不胜感激 第一个活动中的代码: Camera.PictureCallback mPicture = new Camera.PictureCallback() { @Override

我是android编程的新手。我使用两种方式在活动之间传输图像,即使用intent或创建文件,但传输图像并在第二个活动的imageview中显示大约需要3或4秒。有没有什么方法可以比这更快的传输速度,因为很多应用程序(如whatsapp)的传输速度更快。我的代码如下。任何帮助都将不胜感激

第一个活动中的代码:

Camera.PictureCallback mPicture = new Camera.PictureCallback() {
                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {
                        mCamera.stopPreview();
                        Intent myintent = new Intent(CameraSetter.this,CameraPhotoViewer.class);
                        Bitmap bitmap_image = BitmapFactory.decodeByteArray(data, 0, data.length);
                        String fileName = "myImage";//no .png or .jpg needed
                        try {
                            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                            bitmap_image.compress(Bitmap.CompressFormat.JPEG, 50, bytes);
                            FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
                            fo.write(bytes.toByteArray());
                            // remember close file output
                            fo.close();
                            startActivity(myintent);
                        } catch (Exception e) {
                            e.printStackTrace();
                            fileName = null;
                        }

                    }
                };
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            mCamera.stopPreview();
            Intent myintent = new Intent(CameraSetter.this, CameraPhotoViewer.class);
            Bitmap bitmap_image = BitmapFactory.decodeByteArray(data, 0, data.length);
            ContextWrapper cw = new ContextWrapper(getApplicationContext());
            // path to /data/data/yourapp/app_data/imageDir
            File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
            // Create imageDir
            File mypath = new File(directory, "profile.jpg");

            FileOutputStream fos = null;

            try {
                fos = new FileOutputStream(mypath);
                // Use the compress method on the BitMap object to write image to the OutputStream
                bitmap_image.compress(Bitmap.CompressFormat.JPEG, 100, fos);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fos.close();
                    startActivity(myintent);
                    System.out.print(directory.getAbsolutePath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    };
其次:

 Bitmap bitmap_image = BitmapFactory.decodeStream(getApplicationContext().openFileInput("myImage"));
imageview.setImageBitmap(bitmap_image);
这是工作,但我想更快的方式有什么想法

还尝试将映像保存到内部存储器,但这也会占用太多时间

代码是:

在第一项活动中:

Camera.PictureCallback mPicture = new Camera.PictureCallback() {
                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {
                        mCamera.stopPreview();
                        Intent myintent = new Intent(CameraSetter.this,CameraPhotoViewer.class);
                        Bitmap bitmap_image = BitmapFactory.decodeByteArray(data, 0, data.length);
                        String fileName = "myImage";//no .png or .jpg needed
                        try {
                            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                            bitmap_image.compress(Bitmap.CompressFormat.JPEG, 50, bytes);
                            FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
                            fo.write(bytes.toByteArray());
                            // remember close file output
                            fo.close();
                            startActivity(myintent);
                        } catch (Exception e) {
                            e.printStackTrace();
                            fileName = null;
                        }

                    }
                };
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            mCamera.stopPreview();
            Intent myintent = new Intent(CameraSetter.this, CameraPhotoViewer.class);
            Bitmap bitmap_image = BitmapFactory.decodeByteArray(data, 0, data.length);
            ContextWrapper cw = new ContextWrapper(getApplicationContext());
            // path to /data/data/yourapp/app_data/imageDir
            File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
            // Create imageDir
            File mypath = new File(directory, "profile.jpg");

            FileOutputStream fos = null;

            try {
                fos = new FileOutputStream(mypath);
                // Use the compress method on the BitMap object to write image to the OutputStream
                bitmap_image.compress(Bitmap.CompressFormat.JPEG, 100, fos);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fos.close();
                    startActivity(myintent);
                    System.out.print(directory.getAbsolutePath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    };
在第二项活动中:

 imageview = (ImageView) findViewById(R.id.imview_camera_setter);
        framelayout = (FrameLayout) findViewById(R.id.frame_layout_viewer);
        try {


            File f=new File(internalpath, "profile.jpg");
            Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));

            imageview.setImageBitmap(Bitmap.createScaledBitmap(b, 300, 300, false));

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }

仍然需要太多时间

您可以使用单例类来存储位图,而不是使用写/读磁盘(这很慢)。仅在使用位图后,请注意将其清除

从活动A->BitmapSingleton.getInstance().setBitmap(位图)

从活动B->BitmapSingleton.getInstance().getBitmap(位图)

使用后,应该执行BitmapSingleton.getInstance().setBitmap(null);在活动B中

public class BitmapSingleton {

    private Bitmap image = null;

    private static final BitmapSingleton ourInstance = new BitmapSingleton();

    public static BitmapSingleton getInstance() {
        return ourInstance;
    }

    private BitmapSingleton() {
    }

    public Bitmap getBitmap() {
        return image;
    }

    public void setBitmap(Bitmap image) {
        this.image = image;
    } }

您可以做的是获取一个存储文件路径的静态变量。然后,您可以在应用程序中的任何位置访问该图像。

您应该将图像保存在文件中,创建一个低分辨率版本作为第二个活动中的预览,直到您完成加载完整版本,然后交换它们。顺便说一句,您正在显示的图像不应该太大,以防止UI无响应或OOM。我应该在应用程序中保存此文件吗@Matpag是的,您可以使用应用程序内部存储文件夹来存储原始文件,也可以使用缓存来存储原始文件previews@MatPag我已经实现了,但也花了很多时间。如果需要,我可以显示代码是的,将代码添加到question@greenapps固定的!对不起,stackoverflow post中的复制/粘贴和编程。现在需要多长时间?谢谢你的回答,但我已经标记了正确的答案