gpt4 book ai didi

java - 如何在 Android 应用程序中将图像保存到某个位置并为 imageView 检索它?

转载 作者:行者123 更新时间:2023-12-02 02:53:25 30 4
gpt4 key购买 nike

我正在Android中构建一个简单的相机应用程序以在imageView中显示它。我可以获得非常模糊的缩略图。所以我有一个为此制作的文件然后它给我文件的位置。当我检查它时,它是 0 kb,这是很好理解的。我的任务是捕获图像并保存在 mCurrentPhotoPath 中,我应该能够在 startActivityForResult()imageView 中显示它

但是应用程序崩溃在校准之前一次又一次。我知道这一行有问题

 Uri photoURI = FileProvider.getUriForFile(MainActivity.this,"com.infolabs.photu", photoFile);

.我已将 Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider",photoFile); 更改为 com.infolabs.photu 。但我不明白其中有什么问题。
请查看我的代码,我有位置 URL 位置,但它不会显示或工作,因为图像为 0KB

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.captureimage);
imageView = (ImageView) findViewById(R.id.imageView);


b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


if (takePictureIntent.resolveActivity(getPackageManager()) != null)
{
// Create the File where the photo should go
File photoFile = null;
try
{
photoFile = createImageFile();
Toast.makeText(MainActivity.this,mCurrentPhotoPath,Toast.LENGTH_LONG).show();
}
catch (IOException ex)
{
Toast.makeText(MainActivity.this,"the file is not created ",Toast.LENGTH_SHORT).show();
}

if (photoFile != null)
{
//this Uri does not working properly
Uri photoURI = FileProvider.getUriForFile(MainActivity.this,"com.infolabs.photu", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
});
}

错误日志:- 致命异常:主要 进程:infolabs.photu,PID:21415 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager,java.lang.String)” 在 android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583) 在 android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557) 在 android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399) 在 infolabs.photu.MainActivity$1.onClick(MainActivity.java:71) 在 android.view.View.performClick(View.java:5269) 在 android.view.View$PerformClick.run(View.java:21556) 在 android.os.Handler.handleCallback(Handler.java:815) 在 android.os.Handler.dispatchMessage(Handler.java:104) 在 android.os.Looper.loop(Looper.java:207) 在 android.app.ActivityThread.main(ActivityThread.java:5776) 在 java.lang.reflect.Method.invoke( native 方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)04-17 16:56:15.085 21415-21425/infolabs.photu I/系统: FinalizerDaemon: 终结对象 = 65

最佳答案

从相机捕获图像并将其保存到SD卡:

cameraBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

try {

File f = createImageFile();//createImageFile() is added.
if (f != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
} catch (IOException IOE) {
IOE.printStackTrace();
}

}
});


private File createImageFile() throws IOException {

String imageFileName = "image";
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
imageFileName = imageFileName + timeStamp.toString();
File albumF = getStorageDir();
File imageF = File.createTempFile(imageFileName, ".jpg", albumF);
return imageF;

}

private File getStorageDir() {
File storageDir = null;
storageDir = new File(Environment.getExternalStorageDirectory(), "/MyApp" );
if (storageDir != null) {
if (!storageDir.mkdirs()) {
if (!storageDir.exists()) {
Log.d("CameraSample", "failed to create directory");
return null;
}
}
}
return storageDir;
}



private File getImageFile() {
String Path = Environment.getExternalStorageDirectory() + "/MyApp";
File f = new File(Path);
File imageFiles[] = f.listFiles();

if (imageFiles == null || imageFiles.length == 0) {
return null;
}

File lastModifiedFile = imageFiles[0];
for (int i = 1; i < imageFiles.length; i++) {
if (lastModifiedFile.lastModified() < imageFiles[i].lastModified()) {
lastModifiedFile = imageFiles[i];
}
}
return lastModifiedFile;
}

//覆盖activity中的方法,读取保存的图像文件并显示在ImageView中:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {

File imageFile = getImageFile();

Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
mImageView.setImageBitmap(bitmap);

}}}

在menifest文件中添加权限:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />

关于java - 如何在 Android 应用程序中将图像保存到某个位置并为 imageView 检索它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43450296/

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