我試圖使用意圖共享存儲在內部存儲器中的圖像。其過程如下「無法附加空文件」 - 將圖像從內部存儲器共享到Gmail時
- 生成佈局
- 保存此位圖在內部
- 內存共享文件的位圖文件使用意圖(Gmail的)
只有空文件似乎在執行時生成(因此,不能連接到Gmail)。我不確定代碼在哪裏開始變得混亂。所以,在這裏不用代碼
方法來生成和保存位圖
public void generateAndSaveBitmap(View layout) {
//Generate bitmap
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache();
Bitmap imageToSave = layout.getDrawingCache();
layout.destroyDrawingCache();
//Create a file and path
File directory = layout.getContext().getDir("images", Context.MODE_PRIVATE);
File fileName = new File(directory, "sharableImage.jpg");
if (fileName.exists())
fileName.delete();
//Compress and save bitmap under the mentioned fileName
FileOutputStream fos = null;
try {
fos = new FileOutputStream(fileName);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// return directory.getAbsolutePath();
}
方法來共享圖像
public void moreShareOptions(Context context) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
File photoFile = new File("sharableImage.jpg");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
context.startActivity(Intent.createChooser(shareIntent, context.getResources().getText(R.string.opsh_share_this)));
}
這兩種方法都放在裏面onClickListener如下
case R.id.more_share_options:
Thread t = new Thread(new Runnable() {
@Override
public void run() {
optionsShareThis.generateAndSaveBitmap(toolbarWebView);
}
});
t.start();
optionsShareThis.moreShareOptions(this);
break;
試圖找到問題。我錯過了什麼嗎? 此外,我很樂意聽取建議(如果有的話)來改進代碼。
問候
編輯:
感謝提約FileProvider
。很高興知道getCacheDir
。但是,即使在使用FileProvider
之後,我的代碼仍在繼續返回空文件。我一直在探索多種資源,但無法生成具有上述佈局的實際圖像的文件。
generateAndSaveBitmap(視圖佈局) - 此方法沒有更改(上面提到的代碼)。
下面是有關FileProvider
代碼AndroidManifest.xml中 - 沒有新的討論
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path" />
</provider>
file_path.xml - 常提起的文件名和路徑
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="sharableImage" path="images/"/>
</paths>
moreShareOptions() -
public void moreShareOptions(Context context) {
// concatenate the internal cache folder with the document its path and filename
final File file = new File(context.getFilesDir(), "images/sharableImage.jpg");
// let the FileProvider generate an URI for this private file
final Uri uri = FileProvider.getUriForFile(context, "com.example.android", file);
// create an intent, so the user can choose which application he/she wants to use to share this file
final Intent intent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
intent = ShareCompat.IntentBuilder.from((Activity) context)
.setType("image/jpg")
.setStream(uri)
.createChooserIntent()
.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);
}else {
intent = ShareCompat.IntentBuilder.from((Activity) context)
.setType("image/jpg")
.setStream(uri)
.createChooserIntent()
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);
}
}
我失蹤了些什麼?我花了數小時尋找獲得必要結果的方法。沒有什麼成果。因此,我發佈了這個代碼,希望其他人能夠指出我出錯的地方。
感謝您提及FileProvider。仍然只有一個空文件。與生成和保存位圖有關的代碼是否有問題?請檢查編輯部分的更新代碼。任何跡象表明我在哪裏搞砸可能實際上有幫助。 – Traveller