gpt4 book ai didi

android - 如何从另一个 Activity 访问文件

转载 作者:行者123 更新时间:2023-11-30 03:47:31 27 4
gpt4 key购买 nike

我尝试从相机拍摄照片,然后我尝试访问在我创建的 fileProvider() 方法的帮助下在另一个 Activity 中创建的这些两个文件已经写在下面,但对我不好的是,它总是在接收点显示我为空。有人可以帮助我如何访问在这里创建的不同 Activity 中的文件吗?

  public class FileGenerator extends Activity {
File image1,image2,image3,image4;
private String imagePath1,imagePath2,imagePath3,imagePath4;

....................
.........................

public void cameraShot() // This method would be called when the user clicks on a button on my activity to make a request of taking pic.
{

ContentValues values = new ContentValues();
Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

//startActivityForResult(intentPicture,ACTION_TAKE_PICTURE);

Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

if(flipvalue == 0)
{
intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, firstCapturedImageURI);
values.put(MediaStore.Images.Media.TITLE, "testingimage1.jpg");
firstCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
startActivityForResult(intentPicture,CAMERA_DATA_FIRST);
}

if(flipvalue == 1)
{
intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, secondCapturedImageURI);
values.put(MediaStore.Images.Media.TITLE, "testingimage2.jpg");
secondCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
startActivityForResult(intentPicture,CAMERA_DATA_SECOND);


}
}

protected void onActivityResult(int requestCode, int resultCode,Intent data)
{
super.onActivityResult(requestCode, resultCode, data);

if(resultCode==RESULT_OK)
{
Bundle extras= data.getExtras();

if(requestCode == 0)
{
if(bitMap1 != null)
bitMap1.recycle();
bitMap1 = (Bitmap)extras.get("data");
imageView1.setImageBitmap(bitMap1);

selectedImagePath1 = getRealPathFromURI(firstCapturedImageURI);

imagePath1 = selectedImagePath1;
Log.d("Pic Path>>>", selectedImagePath1);

image1 = new File(selectedImagePath1);

flipvalue =1;

}
if (requestCode == 1)
{
if(bitMap2 != null)
bitMap2.recycle();
bitMap2 = (Bitmap)extras.get("data");
imageView2.setImageBitmap(bitMap2);

selectedImagePath2 = getRealPathFromURI(secondCapturedImageURI);

imagePath2 = selectedImagePath2;
Log.d("Pic Path>>>", selectedImagePath2);
image2 = new File(selectedImagePath2);

flipvalue =2;

}
}

protected File fileProvider(int i)
{
if(i==1)
return image1;
else
return image2;


}

public String getRealPathFromURI(Uri contentUri)
{
try
{
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
catch (Exception e)
{
return contentUri.getPath();
}
}
}

以下是我尝试接收这些文件的 Activity 。

public class FileReceiver extends Activity{
File receivedimage1, receivedimage2;
FileGenerator filegen = new FileGenerator();
..........................
............................

receivedimage1 = filegen.fileProvider(1);
Log.d("File 1 ","file1 object"+receivedimage1);// always shows null

receivedimage2 = filegen.fileProvider(2);
Log.d("File 2 ","file2 object"+receivedimage2);// shows null

}

P.S. -- 在尝试此操作之前,我曾经将字符串(即 selectedImagePath1、selectedImagePath2)带到另一个 Activity 中,然后使用这些字符串创建文件。但不知道为什么这会以某种方式破坏我创建的图像文件。所以,我按照上面的代码方式,仍然没有运气。

最佳答案

您将需要使用 Intent 将文件路径发送到其他 Activity,如果您有两个以上的 Activity,则使用 SharedPreferences用于在 SharedPreferences 的其他 Activity 中存储文件路径和检索路径。您可以将 SharedPreferences 中的路径存储为:

 SharedPreferences pathPrefs = FileGenerator.this.getSharedPreferences(
"filepathPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = pathPrefs.edit();
prefsEditor.putString("image1",image1.getAbsolutePath().toString());
prefsEditor.putString("image2",image2.getAbsolutePath().toString());
prefsEditor.commit();

并且在其他 Activity 中从 SharedPreferences 获取文件路径为:

SharedPreferences pathPrefs = 
this.getSharedPreferences("filepathPrefs", MODE_WORLD_READABLE);
String image1 = pathPrefs.getString("image1", "defaultpath");
String image2 = pathPrefs.getString("image2", "defaultpath");
// now use both file path in your code..

关于android - 如何从另一个 Activity 访问文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14729146/

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