android - 在新 Activity 中显示捕获的图像

标签 android

我是 Android 新手,正在构建一个小应用程序,可以从相机拍照并将其保存到图库。

这里是捕获图像的函数。

private void onCaptureImageResult(Intent data) {
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

        File destination = new File(Environment.getExternalStorageDirectory(),
                System.currentTimeMillis() + ".jpg");

        FileOutputStream fo;
        try {
            destination.createNewFile();
            fo = new FileOutputStream(destination);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

这是activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp" >

        <Button
            android:id="@+id/btnSelectPhoto"
            android:background="#149F82"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Select Photo" />
    </LinearLayout>



</LinearLayout>

我想要做什么,当捕获图像时,我想在另一个 Activity (页面)上显示图像,而不是在具有捕获图像按钮的同一 Activity 上。如何做到这一点。

提前致谢

最佳答案

您只需从方法中传递新 Activity 的路径即可。

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("MyImagePath", destination.getAbsoluteFile());
startActivity(intent);

在新 Activity 中

File imgFile = new  File(filePath);

if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

    myImage.setImageBitmap(myBitmap);

}

关于android - 在新 Activity 中显示捕获的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35264908/

相关文章:

android - 更改微调器背景颜色但保留箭头

android - `PreferenceScreen`需要高度和宽度吗?

Android TextView `ifAtLineStart` 方法?

android - 如何在 Android 客户端和服务器端应用程序之间安全地发送密码?

android - 适用于 Android 的 Google Drive Web 服务 API - 显示文件夹中的文件名

android - 键盘显示后,BottomSheetDialog中的Listview不会调整大小

java - 如何在 android 中以编程方式添加或删除 intent 过滤器?

android - 在 AltBeacon 中从后台模式到前台模式的转换是随机的

android - 根据 Cursor 的列值隐藏 ListView 项的元素

android - 如何将 AdMob 横幅放在高度为 MATCH_PARENT 的 View 下方?