gpt4 book ai didi

android - 无法在 android studio 的屏幕上显示和保存捕获的图像

转载 作者:太空狗 更新时间:2023-10-29 13:16:15 26 4
gpt4 key购买 nike

当我只显示捕获的图像时,我的代码工作正常,当我添加代码以将捕获的图像保存在图库中时,它不起作用。

public class MainActivity extends Activity {
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;
ImageView iv;
Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imgView);
Button b = (Button) findViewById(R.id.buttonLoadPicture);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String path = Environment.getExternalStorageDirectory() + "/CameraImages/example.jpg";
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 1);
}
});
}

Bitmap bmp;

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (data != null && data.getExtras() != null) {
bmp = (Bitmap) data.getExtras().get("data");

iv.setImageBitmap(bmp);
}

}
}

请帮帮我,我很震惊

编辑:这是我的日志

12-23 12:44:07.701 8180-8180/com.prgguru.example D/dalvikvm﹕ Late-enabling CheckJNI 
12-23 12:44:07.741 8180-8180/com.prgguru.example D/ActivityThread﹕ handleBindApplication:com.prgguru.example
12-23 12:44:07.741 8180-8180/com.prgguru.example D/ActivityThread﹕ setTargetHeapUtilization:0.25
12-23 12:44:07.741 8180-8180/com.prgguru.example D/ActivityThread﹕ setTargetHeapMinFree:2097152
12-23 12:44:07.751 8180-8180/com.prgguru.example D/ActivityThread﹕ setTargetHeapConcurrentStart:2097152
12-23 12:44:07.761 8180-8180/com.prgguru.example V/HwPolicyFactory﹕ : success to get AllImpl object and return....
12-23 12:44:07.781 8180-8180/com.prgguru.example V/HwWidgetFactory﹕ : successes to get AllImpl object and return....
12-23 12:44:07.931 8180-8180/com.prgguru.example I/Adreno-EGL﹕ : EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LNX.LA.3.7.2.1_RB1.04.04.04.157.009_msm8916_32_LNX.LA.3.7.2.1_RB1__release_AU () OpenGL ES Shader Compiler Version: E031.24.02.07 Build Date: 07/30/14 Wed Local Branch: mybranch3995205 Remote Branch: quic/LNX.LA.3.7.2.1_rb1 Local Patches: NONE Reconstruct Branch: AU_LINUX_ANDROID_LNX.LA.3.7.2.1_RB1.04.04.04.157.009 + NOTHING
12-23 12:44:07.951 8180-8180/com.prgguru.example D/OpenGLRenderer﹕ Enabling debug mode 0
12-23 12:44:08.011 8180-8180/com.prgguru.example I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41d32720 time:84097175
12-23 12:44:14.891 8180-8180/com.prgguru.example V/AudioManager﹕ playSoundEffect effectType: 0
12-23 12:44:14.891 8180-8180/com.prgguru.example V/AudioManager﹕ querySoundEffectsEnabled...
12-23 12:44:14.891 8180-8180/com.prgguru.example D/StubController﹕ holdAndGetPermissionType permissionType:1024 uid:10095 pid:8180
12-23 12:44:14.891 8180-8180/com.prgguru.example D/StubController﹕ addRequestCount, mRequestCount =1 mPhoneIDRequestCount: 0 mLocationRequestCount: 0 permissionType is: 1024
12-23 12:44:14.891 8180-8180/com.prgguru.example D/StubController﹕ holdForGetPermissionSelection mRequestCount:1
12-23 12:44:14.901 8180-8180/com.prgguru.example D/StubController﹕ beforeShowDialogCheckResult:1
12-23 12:44:14.901 8180-8180/com.prgguru.example D/StubController﹕ minusRequestCount, mRequestCount =0 mPhoneIDRequestCount: 0 mLocationRequestCount: 0 permissionType is: 1024
12-23 12:44:20.441 8180-8180/com.prgguru.example I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41d32720 time:84109606
12-23 12:44:23.131 8180-8180/com.prgguru.example I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41d32720 time:84112293

我的 XML这是我的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ImageView>

<Button
android:id="@+id/buttonLoadPicture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0"
android:onClick="loadImagefromGallery"
android:text="Load Picture" >
</Button>

最佳答案

此代码适用于使用 Camera 捕获图像,然后将其存储。如果您想将图片存储到特定位置,您可以进行适当的更改。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
assert thumbnail != null;
thumbnail.compress(Bitmap.CompressFormat.PNG, 100, stream);

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

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

关于android - 无法在 android studio 的屏幕上显示和保存捕获的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34429986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com