【发布时间】:2016-02-08 18:32:53
【问题描述】:
我是 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>
我想在捕获图像时执行此操作,我想在另一个活动(页面)上显示图像,而不是在具有捕获图像按钮的同一活动上。如何做到这一点。
提前致谢
【问题讨论】:
-
您需要将图像保存到设备而不是获取捕获图像的路径stackoverflow.com/questions/20327213/… 并将其传递给所需的活动
-
您已经将图像保存在外部存储
File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");中,只需切换活动并在那里读取文件。 -
从 onactivityResult 中的选取器意图中获取返回的数据 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { Bitmap photo = (Bitmap) data.getExtras().get("数据"); Uri tempUri = getImageUri(getApplicationContext(), photo);然后使用 bundle 将该数据传递给新的 Activity,然后显示该 bundle 中的图像。
-
@Murtaza Khursheed Hussain 如何切换活动,请您详细说明。
-
检查我的答案并告诉我是否可行
标签: android