Android 在电子邮件中发送位图图像
我查看了类似的问题,并试图编写这个简单的代码。 此代码获取视图的位图图像,然后允许用户将其附加到电子邮件。 由于某些原因,我在电子邮件应用程序中收到消息“无法附加空文件”。 我对这方面还不太熟悉,我读了一些例子,尝试了不同的方法,但都没有成功。我怀疑我的道路可能有问题Android 在电子邮件中发送位图图像,android,Android,我查看了类似的问题,并试图编写这个简单的代码。 此代码获取视图的位图图像,然后允许用户将其附加到电子邮件。 由于某些原因,我在电子邮件应用程序中收到消息“无法附加空文件”。 我对这方面还不太熟悉,我读了一些例子,尝试了不同的方法,但都没有成功。我怀疑我的道路可能有问题 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asamater.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
}清单需求:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.asamater.myapplication" >
<!-- Permissions need to be added too-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
明显需要:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.asamater.myapplication" >
<!-- Permissions need to be added too-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这工作
public void email(View view){
ScrollView z = (ScrollView) findViewById(R.id.scrollView);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
Bitmap bitmap = getBitmapFromView(z,totalHeight,totalWidth);
File file = BitmapSaver.saveImageToExternalStorage(this, bitmap);
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "ammar5001@gmail.com");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "report");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, " ");
emailIntent.setType("image/*"); // accept any image
//attach the file to the intent
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(Intent.createChooser(emailIntent, "Send your email in:"));
}
BitmapSaver类:
/**
* Created by ASamater on 01/09/2015.
*/
public class BitmapSaver {
public static File saveImageToExternalStorage(Context context, Bitmap finalBitmap) {
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
long n = System.currentTimeMillis() / 1000L;
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(context, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
return file;
}
}这项工作
public void email(View view){
ScrollView z = (ScrollView) findViewById(R.id.scrollView);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
Bitmap bitmap = getBitmapFromView(z,totalHeight,totalWidth);
File file = BitmapSaver.saveImageToExternalStorage(this, bitmap);
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "ammar5001@gmail.com");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "report");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, " ");
emailIntent.setType("image/*"); // accept any image
//attach the file to the intent
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(Intent.createChooser(emailIntent, "Send your email in:"));
}
BitmapSaver类:
/**
* Created by ASamater on 01/09/2015.
*/
public class BitmapSaver {
public static File saveImageToExternalStorage(Context context, Bitmap finalBitmap) {
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
long n = System.currentTimeMillis() / 1000L;
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(context, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
return file;
}
}请将您的代码简化为一个再现问题的最小示例。您没有实际创建文件。文件是如何创建的?我喜欢它,这家伙说,他进行了研究,进行了有效的尝试,似乎有点迷失,但正在尝试。他包括了他创造的密码,但是暴徒们必须投票否决他。请把你的代码减少到一个最小的例子,以再现问题你没有实际创建文件那么文件是如何创建的?我喜欢它,这家伙说他研究过,尝试过,似乎有点迷失,但正在尝试。他包括了他创造的密码,但是暴徒们必须投票否决他。sighok我这样做了,我仍然得到同样的错误。我只是在读更多关于它的东西。那么我需要设置清单文件中存储的权限吗?saveImageToStorage(位图)返回null,因此路径为null。好的,我使用了更新,我仍然在google电子邮件应用程序中看到“无法附加空文件”。另外,变量“bitmapFile”不存在,所以我将其更改为file?应该是别的吗??方法中有三个文件变量,我发现它们仍然是相同的错误。这可能是由于清单中的权限吗?这就是问题所在,非常感谢。威尔试图增加投票,但还没有足够的代表。好的,我这样做了,我仍然得到同样的错误。我只是在读更多关于它的东西。那么我需要设置清单文件中存储的权限吗?saveImageToStorage(位图)返回null,因此路径为null。好的,我使用了更新,我仍然在google电子邮件应用程序中看到“无法附加空文件”。另外,变量“bitmapFile”不存在,所以我将其更改为file?应该是别的吗??方法中有三个文件变量,我发现它们仍然是相同的错误。这可能是由于清单中的权限吗?这就是问题所在,非常感谢。威尔试图增加选票,但还没有足够的代表。