我正在尝试获取视图的图像(约束布局),并通过android send-intent分享它。
我尝试了很多方法,但直到现在都没有奏效。 这是我到目前为止的内容:
public void shareStatsImage(){
constraintLayout.setDrawingCacheEnabled(true);
Bitmap bitmap = constraintLayout.getDrawingCache();
File path = null;
try {
path = saveImageToExternal(generateImageTitle(), bitmap);
} catch (IOException e) {
e.printStackTrace();
}
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/png");
final File photoFile = new File(Objects.requireNonNull(getActivity()).getFilesDir(), Objects.requireNonNull(path).getAbsolutePath());
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share image using"));
}
public static String generateImageTitle(){
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy-hh-mm-ss");
return sdf.format(new Date());
}
public File saveImageToExternal(String imgName, Bitmap bm) throws IOException {
//Create Path to save Image
String appFolder = "test";
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + File.separator + appFolder); //Creates app specific folder
path.mkdirs();
File imageFile = new File(path, imgName+".png"); // Imagename.png
FileOutputStream out = new FileOutputStream(imageFile);
try{
bm.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
out.flush();
out.close();
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(getContext(),new String[] { imageFile.getAbsolutePath() }, null,new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
} catch(Exception e) {
throw new IOException();
}
return imageFile;
}
此解决方案存在多个问题,例如,将图片共享到gmail时出现错误消息(“附件的权限被拒绝”)。将图片上传到Google驱动器时,我只会收到“上传失败的消息”。
一件好事是这些图像似乎出现在手机的图库中,而不是通过意图共享它们时:(
感谢您的帮助!
答案 0 :(得分:1)
将位图转换为Uri
private Uri getImageUri(Context context, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), inImage, "Image Title", null);
return Uri.parse(path);
}
答案 1 :(得分:0)
您可以使用 Uri 发送图片。
Option Explicit
Private Sub table_to_table()
'Declaration
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set wb1 = Workbooks.Open("C:\Documents and Settings\DON\My Documents\testingmacro2.xlsx")
Set wb2 = Workbooks.Open("C:\Documents and Settings\DON\My Documents\testingmacro3.xlsx")
Set ws1 = wb1.Sheets("Name") 'better to use names instead of indices
Set ws2 = wb2.Sheets("Another name") 'if you add/remove sheets, index change occurs
Dim res_lr As Long
Dim lr2 As Long
lr2 = ws2.Cells(Rows.Count, 2).End(xlUp).Row 'find last row in ws2
Dim copyrange As Range
Dim i As Long
For i = 2 To lr2
' we only loop once, for all values in tbl2
' once we run out of things to add, there's no point looping further
' furthermore, it looks like tbl1 and tbl2 both begin on same row
so we can loop through same value
Set copyrange = ws2.Range(Cells(i, 2), Cells(i, 6)).Copy
res_lr = ws2.Cells(Rows.Count, 8).End(xlUp).Row 'find last row in result tbl
ws2.Range(Cells(res_lr, 8), Cells(res_lr, 12)).PasteSpecial xlPasteValues
Set copyrange = ws1.Range(Cells(i, 2), Cells(i, 6)).Copy
' if tbl2 began in another range, simply change to i + start of data row
ws2.Range(Cells(res_lr + 1, 8), Cells(res_lr + 1, 12)).PasteSpecial xlPasteValues
Next i
wb1.Close 'closes wb1 after looping, i'd leave wb2 open to display the results
End Sub