【问题标题】:How to passing Image Gallery from fragment into another activity?如何将图片库从片段传递到另一个活动?
【发布时间】:2020-03-02 09:17:26
【问题描述】:

将图像从我的画廊传递到另一个活动时出现问题。 UI in Camera FragmentUI in Option Activity

上面的 UI 截图。我已经为拍照(打开相机)和将图像传递给 UI 2 及其作品做了,但是从开放画廊它不能,怎么做?

这是我在 CameraFragment

中的代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_kamera, container, false);
    imageView1 = view.findViewById(R.id.imageView1);
    show_graph = view.findViewById(R.id.show_graph);
    btnTakepic = view.findViewById(R.id.btnTakepic);
    Gofilter = view.findViewById(R.id.Gofilter);

btnTakepic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectImage();
        }
    });

Gofilter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), option.class);
            if(imageView1.getDrawable() == null)
            {
                startActivity(intent);
            }else {
                BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView1.getDrawable();
                Bitmap bitmap = bitmapDrawable.getBitmap();
                intent.putExtra("image", bitmap);
             // intent.putExtra("imageUri", imageUri.toString());
                startActivity(intent);
            }//  else {
             //   BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView1.getDrawable();
              // Bitmap imageBitmap = bitmapDrawable.getBitmap();
              // intent.putExtra("image2", imageBitmap);
                // startActivity(intent);
               //    }

        }
    });

  return view;
}

 private ByteArrayInputStream convertDrawable(ImageView image2){
    BitmapDrawable bitmapDrawable = ((BitmapDrawable) image2.getDrawable());
    Bitmap bitmap = bitmapDrawable.getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
 // bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] imageInByte = stream.toByteArray();
    return new ByteArrayInputStream(imageInByte);
}

这是来自我的 OptionActivity

public class option extends AppCompatActivity {

ImageView imageView;
Uri imageUri;

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

    imageView = findViewById(R.id.imageView2);

    Intent intent = getIntent();
     //  Bundle extras = getIntent().getExtras();
    Bitmap bitmap = intent.getParcelableExtra("image");
     //  Bitmap imageUri1 = intent.getParcelableExtra("imageUri");
    if(bitmap!=null){
        imageView.setImageBitmap(bitmap);
    }   
          // else {
           //            Uri imageUri = Uri.parse(extras.getString("imageUri"));
           //            imageView.setImageURI(imageUri);
           //        }
            //        else if (getIntent().getExtras() != null) {
           //            imageUri = Uri.parse(getIntent().getStringExtra("imageUri"));
           //            imageView.setImageURI(imageUri);
          //        }


}
}

抱歉英语不好,希望有人能帮助我,谢谢:)

【问题讨论】:

    标签: javascript java android android-studio


    【解决方案1】:

    您应该只传递照片的Uri。不是整个位图。

      Uri getUri(Bitmap yourBitmapToPass) {
        String path = Environment.getExternalStorageDirectory().toString();
        OutputStream fOut = null;
        File file = new File(path, "tempPhoto.jpg");
        try {
            fOut = new FileOutputStream(file);
            Bitmap pictureBitmap = yourBitmapToPass;
            pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
            fOut.flush();
            fOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Uri.fromFile(file);
    }
    
    
    void open(Intent intent) {
        Uri myUri = Uri.parse(intent.getExtras().getString("imageUri"));
    }
    

    并以这种方式发送:

    intent.putExtra("imageUri", uriToPass.toString());

    我也建议使用缓存directory

    【讨论】:

    • 你能写代码吗?我的脑子已经卡住了哈哈哈,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多