共享图像意图文件不受支持

时间:2017-07-13 07:10:44

标签: android firebase android-intent firebase-realtime-database firebase-storage

当我点击共享图片的共享按钮时,我说我的图像意图有问题,它说文件不支持。

这是我的代码:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private Context context;
    private List<Upload> uploads;
    Upload upload;

    public MyAdapter(Context context, List<Upload> uploads) {
        this.uploads = uploads;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_images, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
         upload = uploads.get(position);

       holder.textViewName.setText(upload.getName());

        Glide.with(context).load(upload.getUrl()).into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        return uploads.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public TextView textViewName;
        public ImageView imageView,shareImage;


        public ViewHolder(View itemView) {
            super(itemView);

           textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            shareImage=(ImageView)itemView.findViewById(R.id.share);

            shareImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);

                    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(upload.getUrl()));
                    shareIntent.setType("image/*");

                   context.startActivity(Intent.createChooser(shareIntent, "Share your memoir via:"));

                }
            });
        }

    }
}

这是我的showImageActivity.java

public class ShowImagesActivity extends Activity {

    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private DatabaseReference mDatabase;
    private ProgressDialog progressDialog;

    //list to hold all the uploaded images
    private List<Upload> uploads;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycler_view);


        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));


        progressDialog = new ProgressDialog(this);
        final FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        uploads = new ArrayList<>();

        //displaying progress dialog while fetching images
        progressDialog.setMessage("Loading your memories ...");
        progressDialog.show();
        mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);

        //adding an event listener to fetch values
        mDatabase.child(currentFirebaseUser.getUid()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                //dismissing the progress dialog
                progressDialog.dismiss();

                //iterating through all the values in database
                for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                    Upload upload = postSnapshot.getValue(Upload.class);
                    uploads.add(upload);
                }
                //creating adapter
                adapter = new MyAdapter(ShowImagesActivity.this, uploads);

                //adding adapter to recyclerview
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                progressDialog.dismiss();
            }
        });

    }
    }

我正在使用firebase。在upload.getUrl中,图像url保存在firebase数据库中,而图像实际保存在firebase存储中。

这里我正在存储图片

  final FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
            TitleName=title.getText().toString();
            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {

                progressDialog.setMessage("Uploading .....");
                progressDialog.show();
                Uri uri = data.getData();
                StorageReference filePath = storageReference.child(Constants.STORAGE_PATH_UPLOADS).child(currentFirebaseUser.getUid()).child(uri.getLastPathSegment());

                filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        Uri downloadPath = taskSnapshot.getDownloadUrl();
                        Glide.with(MainActivity.this).load(downloadPath).fitCenter().into(imageView);

                        Toast.makeText(getApplicationContext(), "Memory Uploaded", Toast.LENGTH_SHORT).show();
                        progressDialog.dismiss();
                        //creating the upload object to store uploaded image details
                        Upload upload = new Upload(taskSnapshot.getDownloadUrl().toString(), TitleName);

                        //adding an upload to firebase database
                       String uploadId = mDatabase.push().getKey();
                        mDatabase.child(currentFirebaseUser.getUid()).child(uploadId).setValue(upload);
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        progressDialog.dismiss();
                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();

                    }
                });

            }

1 个答案:

答案 0 :(得分:0)

您需要在共享之前保存图像。有一个示例代码用于保存imageview的绘图缓存并共享它。希望它有所帮助。

只需编辑您的代码:

shareImage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        share(imageview);
    }
});

public Bitmap capture(View view) {
    view.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);
    return bitmap;
}

public void share(View view) {
    Context context = view.getContext();
    Bitmap bitmap = capture(view);
    try {
        File file = new File(context.getExternalCacheDir(), DateUtil.format(new Date(), "yyyyMMdd_HHmm") + ".png");
        FileOutputStream fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("image/png");
        Uri uri = Uri.fromFile(file);
        i.putExtra(Intent.EXTRA_STREAM, uri);
        context.startActivity(Intent.createChooser(i, "Share"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}