我的应用程序功能就像我想要使用齐射显示从Json到自定义适配器的几个图像,现在我需要的是当用户单击下载按钮时将该特定片段上的所有显示图像下载到外部存储器。
我已经允许显示文件:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
使用毕加索提取图像和下载的代码是:
public class AKVSeriesOneFragment extends Fragment {
private static final String TAG = MainActivity.class.getSimpleName();
private View view;
// Movies json url
private static final String url = "https://developers.kurtisgallery.in/example_api/kurtisone/id/8";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private GridView gridView;
private CustomListAdapter adapter;
public AKVSeriesOneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_akvseries_one, container, false);
ImageView imageView = (ImageView) rootView.findViewById(R.id.downoadimgs);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < movieList.size(); i++) {
String url = movieList.get(i).getThumbnailUrl();
Picasso.with(getContext())
.load(url)
.into(new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File folder = new File(sd, "/Picasso/");
if (!folder.exists()) {
if (!folder.mkdir()) {
Log.e("ERROR", "Cannot create a directory!");
} else {
folder.mkdir();
}
}
for (int i=0; i< movieList.size(); i++) {
File[] fileName = {new File("Image"+i)};
if (!fileName[i].exists()) {
try {
fileName[i].createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
FileOutputStream outputStream = new FileOutputStream(String.valueOf(fileName[i]));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}
}
});
gridView = (GridView) rootView.findViewById(R.id.list);
adapter = new CustomListAdapter(getActivity(), movieList);
gridView.setAdapter(adapter);
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
//movie.setTitle(obj.getString("category_name"));
movie.setThumbnailUrl(obj.getString("thumb_img_dest"));
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
return rootView;
}
Here I am getting error EROFS (Read-only file system) 我之前搜索过stackoverflow并发现了许多类似的问题,但是没有解决方案对我有用。我是初学Android开发人员。
答案 0 :(得分:0)
我解决了我的问题,因为代码是正确的但由于mashmallow的许可而产生了问题。所以我做了这段代码:
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
//persmission method.
public static void verifyStoragePermissions(Activity activity) {
// Check if we have read or write permission
int writePermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int readPermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
if (writePermission != PackageManager.PERMISSION_GRANTED || readPermission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
并在活动文件中调用函数
verifyStoragePermissions(this);
希望有人帮助!!