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

时间:2017-05-26 13:12:36

标签: android android-intent android-activity android-bitmap

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

代码在第一项活动中:

public class naamEnBriefSelecter extends JFrame implements ActionListener {
private String ingevoerdeCodnummer;
private String soortBriefIngevoerd;
private JLabel codNR;
private JLabel soortBrief;
private JTextField invoerCodnummer;
private JComboBox<String> cb;
private int keuze;
private boolean klaar;





public void GUI(Container paneel) {
    klaar = false;

    paneel.setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();

    //JLabel
    codNR = new JLabel("Geef hier het Codicillen nummer op");
    soortBrief = new JLabel("Geef hier het soortBrief op");

    invoerCodnummer = new JTextField(20);
    String[] brieven = {"test1", "test2", "test3", "test4"};
    cb = new JComboBox<String>(brieven);

    JButton opslaan = new JButton("Opslaan");

    constraints.anchor = GridBagConstraints.WEST;
    constraints.insets = new Insets(10, 10, 10, 10);

    // add components to the panel
    constraints.gridx = 0;
    constraints.gridy = 0;
    paneel.add(codNR, constraints);

    constraints.gridx = 1;
    paneel.add(invoerCodnummer, constraints);

    constraints.gridx = 0;
    constraints.gridy = 1;
    paneel.add(soortBrief, constraints);

    constraints.gridx = 1;
    paneel.add(cb, constraints);

    constraints.gridx = 0;
    constraints.gridy = 2;
    constraints.gridwidth = 2;
    constraints.anchor = GridBagConstraints.CENTER;
    paneel.add(opslaan, constraints);



        opslaan.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

    keuze = JOptionPane.showConfirmDialog(null, "De volgende gegevens zijn ingevoerd: " + invoerCodnummer.getText() + " als codicillen nummer en de volgende soort brief: "
           + cb.getSelectedItem().toString() + "\n" + "Kloppen deze gegevens?");
   if (keuze == 0) {
        setter(invoerCodnummer.getText());
      System.out.println(invoerCodnummer.getText());
      System.exit(0);
   }


}


public void setter(String codnr) {
    this.ingevoerdeCodnummer = codnr;

}

public String getCodNr() {
    return ingevoerdeCodnummer;
}

public void laatZien() {
    //Create and set up the window.

    JFrame frame = new JFrame("Naam en Soort Brief selecteer");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    GUI(frame.getContentPane());

    //Display the window.
    frame.pack();
    frame.setVisible(true);

}



public static void main(String[] args) {
    naamEnBriefSelecter a = new naamEnBriefSelecter();           
        a.laatZien();       
    System.out.println(a.getCodNr()); // here should be the value comming from ingevoerdeCodnummer

}
}

和第二名:

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

                    }
                };

它正在运作,但我希望更快地获得任何想法?

还尝试将图像保存到内部存储器,但也花费了太多时间。

代码是:

在第一项活动中:

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

        }
    };

仍然需要太多时间

2 个答案:

答案 0 :(得分:0)

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

来自活动A - &gt; BitmapSingleton.getInstance()setBitmap(位图);

来自活动B - &gt; 。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;
    } }

答案 1 :(得分:0)

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

相关问题