![](/img/trans.png)
[英]How to send image from one activity to another activity that is selected from the android Gallery?
[英]how to send image from one activity to another taken from gallery?
我是 android 的初學者,我似乎無法找到這個問題的解決方案。
我想將一個活動的圖像發送到從畫廊拍攝的另一個活動,但無論我做什么都不起作用,我將不勝感激。 從圖庫中選擇圖像的代碼
主要活動
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void SendImage(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("resId", R.drawable.image);
startActivity(intent);
}
}
第二個Activity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class SecondActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
imageView = findViewById(R.id.imageView2);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
int resId = bundle.getInt("resId");
imageView.setImageResource(resId);
}
}
}
您不需要在活動之間傳遞圖像。如果您的可繪制文件夾中有該圖像,您可以直接在任何活動中使用它。如下所示
ImageView iv =(ImageView) findVIewByID(R.id.image);
iv.setImage(getresources.getDrawable(R.drawable.filename);
否則您可以在 Imageview 上使用 xml 屬性,例如
android:src="@drawable/filename"
將此行添加到您的圖像視圖中。
如果你有 imageurl,你可以作為一個字符串傳遞。你可以從第一個活動的意圖中傳遞一個字符串,你可以從下一個活動中獲取它。然后使用該 url 可以填充圖像視圖。
對於捕獲圖像或從圖庫中選擇圖像首先您需要許可。 獲得許可后,您可以從圖庫中選擇圖像,然后您可以為所欲為。 這是完整示例的鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.