为网格图像提供共享选项
Posted
技术标签:
【中文标题】为网格图像提供共享选项【英文标题】:Giving share option to the grid images 【发布时间】:2020-07-10 16:13:33 【问题描述】:我正在创建一个应用程序,我想将图像分享到任何社交平台。我成功地使用了网格视图。我想为获取图像位置并分享到社交平台的应用程序添加社交分享功能。这个方法我试过了
BitmapDrawable bm = (BitmapDrawable) yourImageView.getDrawable();
Bitmap mysharebmp = bm.getBitmap();
String path = Images.Media.insertImage(getContentResolver(),
mysharebmp, "MyImage", null);
Uri uri = Uri.parse(path);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(sharingIntent,
"Share image using"));
它给出了致命错误当从 gridView 中单击时如何共享图像?我来了
java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'android.graphics.drawable.Drawable android.widget.ImageView.getDrawable()' 我收到此错误
gridView = (GridView) findViewById(R.id.gridView);
list = new ArrayList<>();
adapter = new FoodListAdapter(this, R.layout.food_items, list);
gridView.setAdapter(adapter);
// get all data from sqlite
Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT * FROM FOOD");
list.clear();
while (cursor.moveToNext())
int id = cursor.getInt(0);
String name = cursor.getString(1);
String price = cursor.getString(2);
byte[] image = cursor.getBlob(3);
list.add(new Food(name, price, image, id));
adapter.notifyDataSetChanged();
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
);
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
if(requestCode == 888)
if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 888);
else
Toast.makeText(getApplicationContext(), "You don't have permission to access file location!", Toast.LENGTH_SHORT).show();
return;
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if(requestCode == 888 && resultCode == RESULT_OK && data != null)
Uri uri = data.getData();
try
InputStream inputStream = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageViewFood.setImageBitmap(bitmap);
catch (FileNotFoundException e)
e.printStackTrace();
super.onActivityResult(requestCode, resultCode, data);
【问题讨论】:
能否请您添加来自 logcat 的堆栈跟踪,以便我知道错误来自哪里? 单击某个按钮时是否出现空指针异常? 我用 recyclerview 修复了 那么请分享您的答案并接受您自己的答案作为正确答案:)\ 【参考方案1】:在 onItemClickListener 中调用 startActivity。您必须在 clickListener 中插入获取 Image 位置的逻辑,将该路径作为额外的附加到意图。
【讨论】:
`java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.graphics.drawable.Drawable android.widget.ImageView.getDrawable()' 我得到了这个错误 请用新代码更新问题中的代码,以便我知道您做了什么。干杯!!以上是关于为网格图像提供共享选项的主要内容,如果未能解决你的问题,请参考以下文章
是否有任何方法可以在将图像上传到我的 tkinter fyi 窗口之前为用户提供调整图像大小的选项