单击图像时,适配器中有图像视图列表,我显示全屏图像。我在适配器外部有一个按钮。
现在,我想在按钮的onClick片段中获取该图像以共享该图像。 下面的代码在适配器中下载我的图像。
我已经使用Android-Universal-Image-Loader库显示图像。
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if(callback != null) {
Bitmap bitmap = ((BitmapDrawable) img_main_bg.getDrawable()).getBitmap();
callback.onItemClicked(bitmap);
}
spinner.setVisibility(View.GONE);
}
如何获取片段图像?
我已经尝试过界面,但是onLoadingComplete一次下载了多个图像,所以我无法获得正确的图像。
答案 0 :(得分:1)
您可以按照以下方式进行操作
首先在该点击侦听器中请求保存图像的权限
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
imgView = imageView;
boolean hasPermission = (ContextCompat.checkSelfPermission(ImagePagerActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
ActivityCompat.requestPermissions(ImagePagerActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
112);
}else
{
showDialog();
}
}
});
如果应用了权限,则先保存图像然后再共享
private void showDialog()
{
new AlertDialog.Builder(ImagePagerActivity.this,R.style.MyAlertDialogStyle)
.setTitle("Select your option")
.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
imgView.buildDrawingCache();
Bitmap bm = imgView
.getDrawingCache();
OutputStream fOut = null;
try {
File root = new File(
Environment
.getExternalStorageDirectory()
+ File.separator
+ "Beauty"
+ File.separator);
if (!root.exists())
root.mkdirs();
File sdImageMainDirectory = new File(
root,
System.currentTimeMillis()
+ ".jpg");
fOut = new FileOutputStream(
sdImageMainDirectory);
bm.compress(
Bitmap.CompressFormat.PNG,
100, fOut);
fOut.flush();
fOut.close();
Toast.makeText(
ImagePagerActivity.this,
"File saved at Beauty folder",
Toast.LENGTH_SHORT)
.show();
} catch (Exception e) {
Toast.makeText(
ImagePagerActivity.this,
"Error occured. Please try again later.",
Toast.LENGTH_SHORT)
.show();
e.printStackTrace();
}
}
})
.setNegativeButton("Share",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
try {
imgView.buildDrawingCache();
Bitmap bm = imgView
.getDrawingCache();
OutputStream fOut = null;
File root = new File(
Environment
.getExternalStorageDirectory()
+ File.separator
+ " Beauty"
+ File.separator);
if (!root.exists())
root.mkdirs();
File sdImageMainDirectory = new File(
root, "1.jpg");
fOut = new FileOutputStream(
sdImageMainDirectory);
bm.compress(
Bitmap.CompressFormat.PNG,
100, fOut);
fOut.flush();
fOut.close();
Intent shareIntent = new Intent(
Intent.ACTION_SEND);
Uri phototUri = Uri
.fromFile(sdImageMainDirectory);
shareIntent.setData(phototUri);
shareIntent
.setType("image/png");
shareIntent.putExtra(
Intent.EXTRA_STREAM,
phototUri);
startActivityForResult(Intent
.createChooser(
shareIntent,
"share using"),
2);
} catch (Exception ce) {
ce.printStackTrace();
}
}
})
.show();
}