我用这种方法保存了2年的图像,没有我改变任何东西,这不再适用于我了。新版本似乎删除了这个方法,将我的android studio应用程序中的图像保存到手机的多媒体资料中。现在,画廊里什么都没有。我使用api 29,得到以下错误:
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/MyAppName/coolimage.jpg: open failed: ENOENT (No such file or directory)
这是我的密码:
package com.xxx.xxxxx;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.xxx.xxxxx.R;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;
/**
* A simple {@link Fragment} subclass.
*/
public class view_skin_fragment extends Fragment {
final int[] imageId = {
R.drawable.s1coolimage
};
final int[] dowloadimageId = {
R.drawable.d1coolimage
};
final String[] skinNames = {
"My image name"} ;
final String[] skinDescription ={
"Image description",
};
SQLiteDatabase db;
private int imageindedx;
private static final int START_LEVEL = 1;
private int mLevel;
private Button mNextLevelButton;
private TextView mLevelTextView;
public view_skin_fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView= inflater.inflate(R.layout.fragment_view_skin, container, false);
Button share=(Button) rootView.findViewById(R.id.share_vs);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Image collection title");
startActivity(Intent.createChooser(sharingIntent, "Share"));
}
});
imageindedx=getArguments().getInt("imageIndex");
ImageView img= (ImageView) rootView.findViewById(R.id.imageView);
TextView skinName=(TextView) rootView.findViewById(R.id.skinname);
Button back = (Button) rootView.findViewById(R.id.back);
Button download = (Button) rootView.findViewById(R.id.download);
skinName.setText(skinNames[imageindedx]);
img.setImageResource(imageId[imageindedx]);
TextView SkinDescription =(TextView) rootView.findViewById(R.id.description);
SkinDescription.setText(skinDescription[imageindedx]);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame, new grid_layout()).commit();
}
});
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Drawable drawable = getActivity().getDrawable(dowloadimageId[imageindedx]);
// Get the bitmap from drawable object
// Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),dowloadimageId[imageindedx] );
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/MyAppName");
if (!myDir.exists()) {
myDir.mkdirs();
}
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = skinNames[imageindedx] + n + ".jpg";
File file = new File(myDir, fname);
Log.i("Fileee", "" + file);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("path",file.getPath());
MediaScannerConnection.scanFile(getActivity(),
new String[] { file.getPath() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
//now visible in gallery
}
}
);
Toast.makeText(getActivity(),getString(R.string.saved_to_gallery),Toast.LENGTH_SHORT).show();
}
});
Button fav = (Button) rootView.findViewById(R.id.fav);
fav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insert();
}
});
return rootView;
}
int id=0;
public void insert(){
id++;
db = new MySQLiteOpenHelper(getActivity()).getWritableDatabase();
ContentValues values = new ContentValues();
// values.put(SQLDemoContract.tables.StudentEntry._ID, "One"+String.valueOf(id));
values.put(SQLDemoContract.tables.StudentEntry.COLUMN_NAME_IMAGE_INDEX, imageindedx );
db.insert(SQLDemoContract.tables.StudentEntry.TABLE_NAME, null, values);
db.close();
Toast.makeText(getActivity(),getString(R.string.added_to_favorites),Toast.LENGTH_SHORT).show();
}
}
暂无答案!
目前还没有任何答案,快来回答吧!