gpt4 book ai didi

java - 获取错误 : cannot resolve method getResource()

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:02:18 35 4
gpt4 key购买 nike

我正在尝试与 Intent.ACTION_SEND 共享图像,但出现此错误:

cannot resolve method getResource()

这是我尝试共享图像的代码:

            File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/myFolder");
dir.mkdirs(); // build directory
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fileName = "Image-"+ n +".jpg";
File file = new File(dir, fileName);
if (file.exists ()) file.delete ();

try {
FileOutputStream outStream = new FileOutputStream(file);
InputStream is;
Bitmap bitmap;
is = this.getResources().openRawResource(R.drawable.image1);
bitmap = BitmapFactory.decodeStream(is);
try {
is.close();
is = null;
} catch (IOException e) {
}
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, outputFileUri);
startActivity(Intent.createChooser(intent, "Share this image via"));

最佳答案

如果你在 fragment 中使用:

getActivity().getResources();

或者尝试:

getApplicationContext().getResources();

如果您在 Activity 中但在内部类中,请编辑此行中的单词 this:

is = this.getResources().openRawResource(R.drawable.image1);

收件人:

is = Your_Activity_Class_Name.this.getResources().openRawResource(R.drawable.image1);

关于java - 获取错误 : cannot resolve method getResource(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47413316/

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