آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

مشکل در کد اشتراک یک عکس در imageView

احسان  10 سال پیش  10 سال پیش
+2 0

سلام ، من یک imageView دارم که از پوشه Drawable به صورت تصادفی یک عکس میاره ، میخوام این عکس رو با یک دکمه ارسال کنم (ارسال عکس در تلگرام و ....) 

خیلی سرچ کردم و این کد جواب میده ولی یک مشکل داره.

در این خط :

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.p1);

فقط یک عکس ثابت رو انتخاب میکنه .

چجور باید کد رو اصلاح کرد که از یک id مربوط به imageview عکس رو بگیره و ارسال کنه؟

 Button btnShare = (Button) findViewById(R.id.btnShare);
		btnShare.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Bitmap bitmap;
				OutputStream output;
				Intent share;
			

				// Decoding the image
				bitmap = BitmapFactory.decodeResource(getResources(),
						R.drawable.p1);

				// Find the SD Card path
				File filepath = Environment.getExternalStorageDirectory();

				// Create a new folder AndroidBegin in SD Card
				File dir = new File(filepath.getAbsolutePath()
						+ "/AndroidBegin/");
				dir.mkdirs();

				// Create a name for the saved image
				File file = new File(dir, "sample_wallpaper.jpg");

				try {

					// Start Share Intent
					share = new Intent(Intent.ACTION_SEND);

					// Type of file to share
					share.setType("image/jpeg");

					// Saves the file into SD Card
					output = new FileOutputStream(file);

					// Compress into png format image from 0% - 100%, using 100%
					// for this tutorial
					bitmap.compress(Bitmap.CompressFormat.JPEG, 90, output);
					output.flush();
					output.close();

					// Locate the image to Share
					Uri uri = Uri.fromFile(file);

					// Captures the share image
					share.putExtra(Intent.EXTRA_STREAM, uri);

					// Start the share dialog
					startActivity(Intent.createChooser(share, "Share Image"));
				}

				// Catch exceptions
				catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});

	}

 

0 0
(10 سال پیش)
0 0
من از کدت استفاده کردم عالی بود.خدا کمکت کنه (10 سال پیش)
 برای این سوال 2 پاسخ وجود دارد.
پاسخ به سوال 
Hossein Lamei Ramandi  10 سال پیش
+3 0

بفرما

نمیدونم منظورتون رو درست متوجه شدم یا نه ولی این کد یه عکسو از ایمیج ویو شیر میکنه

 		img = (ImageView)findViewById(R.id.imageView1);
(Button) btn = (Button)findViewById(R.id.btn);


btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

String fileName = "image.jpg";
img.setDrawingCacheEnabled(true);
Bitmap bitmap = img.getDrawingCache();
try
{
FileOutputStream ostream = MainActivity.this.openFileOutput( fileName, Context.MODE_WORLD_READABLE);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_SUBJECT, "Great photo!");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile( new File( MainActivity.this.getFileStreamPath( fileName).getAbsolutePath())));
startActivity(Intent.createChooser(share,"Share via")); }
});

}
0 0
سلام این کدی شما دادی اسکرین شات میگیره ، من میخوام عکسی که یک imageview داره رو اشتراک بذارم ، این اسکرین شات میگیره و عرض و ارتفاع خیللی بزرگ میشه و اصلا اندازه واقعی عکسه نیست (10 سال پیش)
پاسخ به سوال 
محمد  10 سال پیش
+1 0

 int id = getResources().getIdentifier(fileName, "raw", getPackageName());
 bitmap = BitmapFactory.decodeResource(getResources(), id);

کد بالا id رو بصورت داینامیک به bitmap میفرسته


پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .