我有这个代码将图像从viewpager保存到SD卡但是当我运行它时#34;图像保存期间出错"出现了。 我读到了这个:How to save image from ViewPager? 并且做了所有的评论和每一个错误都没有了,但是当我运行它时#34;图像保存期间出错"出现了。
Sdcard.java
package com.example.sdcard;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class Sdcard extends Activity implements OnClickListener{
Context mContext ;
Button save;
final File myDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/Images/");
boolean success = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sdcard);
final Context mContext ;
mContext=this;
save = (Button) findViewById(R.id.bSave);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
final ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
final String fname = "StyleMe-" + n + ".png";
myDir.mkdirs();
File image = new File(myDir, fname);
int currentItem =viewPager.getCurrentItem();
Drawable drawable = mContext.getResources().getDrawable(adapter.mImages[currentItem]);
Bitmap bitmap =((BitmapDrawable) drawable).getBitmap();
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success at /sdcard/Pictures/SexyImages",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse
("file://"
+ Environment.getExternalStorageDirectory())));
}
});
}
public class ImagePagerAdapter extends PagerAdapter {
public int[] mImages = new int[] {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
};
@Override
public int getCount() {
return mImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Sdcard.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.activity_horizontal_margin);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
和sdcard.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true" >
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/bSave"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text="Save"
android:layout_alignParentLeft="true"
android:textSize="24sp"
android:padding="0dp"
/>
</RelativeLayout>
为什么出现这个错误???? 请帮帮我
它是logcat红线:
03-28 03:59:17.556: E/AndroidRuntime(1449): FATAL EXCEPTION: main
03-28 03:59:17.556: E/AndroidRuntime(1449): Process: com.example.sdcard, PID: 1449
03-28 03:59:17.556: E/AndroidRuntime(1449): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=1449, uid=10075
03-28 03:59:17.556: E/AndroidRuntime(1449): at android.os.Parcel.readException(Parcel.java:1461)
03-28 03:59:17.556: E/AndroidRuntime(1449): at android.os.Parcel.readException(Parcel.java:1415)
03-28 03:59:17.556: E/AndroidRuntime(1449): at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2373)
03-28 03:59:17.556: E/AndroidRuntime(1449): at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1127)
03-28 03:59:17.556: E/AndroidRuntime(1449): at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:365)
03-28 03:59:17.556: E/AndroidRuntime(1449): at com.example.sdcard.Sdcard$1.onClick(Sdcard.java:92)
03-28 03:59:17.556: E/AndroidRuntime(1449): at android.view.View.performClick(View.java:4424)
03-28 03:59:17.556: E/AndroidRuntime(1449): at android.view.View$PerformClick.run(View.java:18383)
03-28 03:59:17.556: E/AndroidRuntime(1449): at android.os.Handler.handleCallback(Handler.java:733)
03-28 03:59:17.556: E/AndroidRuntime(1449): at android.os.Handler.dispatchMessage(Handler.java:95)
03-28 03:59:17.556: E/AndroidRuntime(1449): at android.os.Looper.loop(Looper.java:137)
03-28 03:59:17.556: E/AndroidRuntime(1449): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-28 03:59:17.556: E/AndroidRuntime(1449): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 03:59:17.556: E/AndroidRuntime(1449): at java.lang.reflect.Method.invoke(Method.java:515)
03-28 03:59:17.556: E/AndroidRuntime(1449): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-28 03:59:17.556: E/AndroidRuntime(1449): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-28 03:59:17.556: E/AndroidRuntime(1449): at dalvik.system.NativeStart.main(Native Method)
03-28 04:00:36.556: E/AndroidRuntime(1498): FATAL EXCEPTION: main
03-28 04:00:36.556: E/AndroidRuntime(1498): Process: com.example.sdcard, PID: 1498
03-28 04:00:36.556: E/AndroidRuntime(1498): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=1498, uid=10075
03-28 04:00:36.556: E/AndroidRuntime(1498): at android.os.Parcel.readException(Parcel.java:1461)
03-28 04:00:36.556: E/AndroidRuntime(1498): at android.os.Parcel.readException(Parcel.java:1415)
03-28 04:00:36.556: E/AndroidRuntime(1498): at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2373)
03-28 04:00:36.556: E/AndroidRuntime(1498): at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1127)
03-28 04:00:36.556: E/AndroidRuntime(1498): at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:365)
03-28 04:00:36.556: E/AndroidRuntime(1498): at com.example.sdcard.Sdcard$1.onClick(Sdcard.java:92)
03-28 04:00:36.556: E/AndroidRuntime(1498): at android.view.View.performClick(View.java:4424)
03-28 04:00:36.556: E/AndroidRuntime(1498): at android.view.View$PerformClick.run(View.java:18383)
03-28 04:00:36.556: E/AndroidRuntime(1498): at android.os.Handler.handleCallback(Handler.java:733)
03-28 04:00:36.556: E/AndroidRuntime(1498): at android.os.Handler.dispatchMessage(Handler.java:95)
03-28 04:00:36.556: E/AndroidRuntime(1498): at android.os.Looper.loop(Looper.java:137)
03-28 04:00:36.556: E/AndroidRuntime(1498): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-28 04:00:36.556: E/AndroidRuntime(1498): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 04:00:36.556: E/AndroidRuntime(1498): at java.lang.reflect.Method.invoke(Method.java:515)
03-28 04:00:36.556: E/AndroidRuntime(1498): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-28 04:00:36.556: E/AndroidRuntime(1498): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-28 04:00:36.556: E/AndroidRuntime(1498): at dalvik.system.NativeStart.main(Native Method)
03-28 04:02:25.736: E/AndroidRuntime(1540): FATAL EXCEPTION: main
03-28 04:02:25.736: E/AndroidRuntime(1540): Process: com.example.sdcard, PID: 1540
03-28 04:02:25.736: E/AndroidRuntime(1540): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=1540, uid=10075
03-28 04:02:25.736: E/AndroidRuntime(1540): at android.os.Parcel.readException(Parcel.java:1461)
03-28 04:02:25.736: E/AndroidRuntime(1540): at android.os.Parcel.readException(Parcel.java:1415)
03-28 04:02:25.736: E/AndroidRuntime(1540): at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2373)
03-28 04:02:25.736: E/AndroidRuntime(1540): at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1127)
03-28 04:02:25.736: E/AndroidRuntime(1540): at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:365)
03-28 04:02:25.736: E/AndroidRuntime(1540): at com.example.sdcard.Sdcard$1.onClick(Sdcard.java:92)
03-28 04:02:25.736: E/AndroidRuntime(1540): at android.view.View.performClick(View.java:4424)
03-28 04:02:25.736: E/AndroidRuntime(1540): at android.view.View$PerformClick.run(View.java:18383)
03-28 04:02:25.736: E/AndroidRuntime(1540): at android.os.Handler.handleCallback(Handler.java:733)
03-28 04:02:25.736: E/AndroidRuntime(1540): at android.os.Handler.dispatchMessage(Handler.java:95)
03-28 04:02:25.736: E/AndroidRuntime(1540): at android.os.Looper.loop(Looper.java:137)
03-28 04:02:25.736: E/AndroidRuntime(1540): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-28 04:02:25.736: E/AndroidRuntime(1540): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 04:02:25.736: E/AndroidRuntime(1540): at java.lang.reflect.Method.invoke(Method.java:515)
03-28 04:02:25.736: E/AndroidRuntime(1540): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-28 04:02:25.736: E/AndroidRuntime(1540): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-28 04:02:25.736: E/AndroidRuntime(1540): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您是否设置了权限?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
java.lang.SecurityException:Permission Denial:不允许从pid = 1498发送广播android.intent.action.MEDIA_MOUNTED,uid = 10075
public static boolean isSDCARDAvailable(){
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)? true :false;
}
和
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />